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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
501 | Contest | [
"implementation"
] | null | null | Misha and Vasya participated in a Codeforces contest. Unfortunately, each of them solved only one problem, though successfully submitted it at the first attempt. Misha solved the problem that costs *a* points and Vasya solved the problem that costs *b* points. Besides, Misha submitted the problem *c* minutes after the contest started and Vasya submitted the problem *d* minutes after the contest started. As you know, on Codeforces the cost of a problem reduces as a round continues. That is, if you submit a problem that costs *p* points *t* minutes after the contest started, you get points.
Misha and Vasya are having an argument trying to find out who got more points. Help them to find out the truth. | The first line contains four integers *a*, *b*, *c*, *d* (250<=β€<=*a*,<=*b*<=β€<=3500, 0<=β€<=*c*,<=*d*<=β€<=180).
It is guaranteed that numbers *a* and *b* are divisible by 250 (just like on any real Codeforces round). | Output on a single line:
"Misha" (without the quotes), if Misha got more points than Vasya.
"Vasya" (without the quotes), if Vasya got more points than Misha.
"Tie" (without the quotes), if both of them got the same number of points. | [
"500 1000 20 30\n",
"1000 1000 1 1\n",
"1500 1000 176 177\n"
] | [
"Vasya\n",
"Tie\n",
"Misha\n"
] | none | [
{
"input": "500 1000 20 30",
"output": "Vasya"
},
{
"input": "1000 1000 1 1",
"output": "Tie"
},
{
"input": "1500 1000 176 177",
"output": "Misha"
},
{
"input": "1500 1000 74 177",
"output": "Misha"
},
{
"input": "750 2500 175 178",
"output": "Vasya"
},
{
"input": "750 1000 54 103",
"output": "Tie"
},
{
"input": "2000 1250 176 130",
"output": "Tie"
},
{
"input": "1250 1750 145 179",
"output": "Tie"
},
{
"input": "2000 2000 176 179",
"output": "Tie"
},
{
"input": "1500 1500 148 148",
"output": "Tie"
},
{
"input": "2750 1750 134 147",
"output": "Misha"
},
{
"input": "3250 250 175 173",
"output": "Misha"
},
{
"input": "500 500 170 176",
"output": "Misha"
},
{
"input": "250 1000 179 178",
"output": "Vasya"
},
{
"input": "3250 1000 160 138",
"output": "Misha"
},
{
"input": "3000 2000 162 118",
"output": "Tie"
},
{
"input": "1500 1250 180 160",
"output": "Tie"
},
{
"input": "1250 2500 100 176",
"output": "Tie"
},
{
"input": "3500 3500 177 178",
"output": "Tie"
},
{
"input": "3000 3250 16 34",
"output": "Tie"
},
{
"input": "1750 3000 137 49",
"output": "Vasya"
},
{
"input": "500 1500 179 71",
"output": "Vasya"
},
{
"input": "1250 2000 101 180",
"output": "Misha"
},
{
"input": "250 750 180 176",
"output": "Vasya"
},
{
"input": "2250 2250 163 145",
"output": "Vasya"
},
{
"input": "3000 3000 176 78",
"output": "Vasya"
},
{
"input": "250 3500 8 178",
"output": "Vasya"
},
{
"input": "1750 1250 179 180",
"output": "Misha"
},
{
"input": "2750 1750 13 164",
"output": "Misha"
},
{
"input": "1750 2250 178 53",
"output": "Vasya"
},
{
"input": "2500 2750 73 179",
"output": "Misha"
},
{
"input": "1000 3500 178 175",
"output": "Vasya"
},
{
"input": "1000 500 7 162",
"output": "Misha"
},
{
"input": "1000 250 175 48",
"output": "Misha"
},
{
"input": "1750 500 166 177",
"output": "Misha"
},
{
"input": "250 250 0 0",
"output": "Tie"
},
{
"input": "250 3500 0 0",
"output": "Vasya"
},
{
"input": "250 3500 0 180",
"output": "Vasya"
},
{
"input": "3500 3500 180 180",
"output": "Tie"
},
{
"input": "3500 250 0 180",
"output": "Misha"
}
] | 31 | 0 | -1 | 1,679 |
|
433 | Kitahara Haruki's Gift | [
"brute force",
"implementation"
] | null | null | Kitahara Haruki has bought *n* apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.
Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given to Ogiso Setsuna.
But unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends? | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100) β the number of apples. The second line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (*w**i*<==<=100 or *w**i*<==<=200), where *w**i* is the weight of the *i*-th apple. | In a single line print "YES" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print "NO" (without the quotes). | [
"3\n100 200 100\n",
"4\n100 100 100 200\n"
] | [
"YES\n",
"NO\n"
] | In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa. | [
{
"input": "3\n100 200 100",
"output": "YES"
},
{
"input": "4\n100 100 100 200",
"output": "NO"
},
{
"input": "1\n100",
"output": "NO"
},
{
"input": "1\n200",
"output": "NO"
},
{
"input": "2\n100 100",
"output": "YES"
},
{
"input": "2\n200 200",
"output": "YES"
},
{
"input": "100\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "YES"
},
{
"input": "100\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "52\n200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 100 200 100 200 200 200 100 200 200",
"output": "YES"
},
{
"input": "2\n100 200",
"output": "NO"
},
{
"input": "2\n200 100",
"output": "NO"
},
{
"input": "3\n100 100 100",
"output": "NO"
},
{
"input": "3\n200 200 200",
"output": "NO"
},
{
"input": "3\n200 100 200",
"output": "NO"
},
{
"input": "4\n100 100 100 100",
"output": "YES"
},
{
"input": "4\n200 200 200 200",
"output": "YES"
},
{
"input": "100\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "YES"
},
{
"input": "100\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 100 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "YES"
},
{
"input": "100\n100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "NO"
},
{
"input": "100\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "YES"
},
{
"input": "100\n100 100 100 100 100 100 100 100 200 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "NO"
},
{
"input": "99\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "99\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "99\n200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "YES"
},
{
"input": "99\n200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "99\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "NO"
},
{
"input": "99\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "YES"
},
{
"input": "99\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100",
"output": "NO"
},
{
"input": "100\n100 100 200 100 100 200 200 200 200 100 200 100 100 100 200 100 100 100 100 200 100 100 100 100 100 100 200 100 100 200 200 100 100 100 200 200 200 100 200 200 100 200 100 100 200 100 200 200 100 200 200 100 100 200 200 100 200 200 100 100 200 100 200 100 200 200 200 200 200 100 200 200 200 200 200 200 100 100 200 200 200 100 100 100 200 100 100 200 100 100 100 200 200 100 100 200 200 200 200 100",
"output": "YES"
},
{
"input": "100\n100 100 200 200 100 200 100 100 100 100 100 100 200 100 200 200 200 100 100 200 200 200 200 200 100 200 100 200 100 100 100 200 100 100 200 100 200 100 100 100 200 200 100 100 100 200 200 200 200 200 100 200 200 100 100 100 100 200 100 100 200 100 100 100 100 200 200 200 100 200 100 200 200 200 100 100 200 200 200 200 100 200 100 200 200 100 200 100 200 200 200 200 200 200 100 100 100 200 200 100",
"output": "NO"
},
{
"input": "100\n100 200 100 100 200 200 200 200 100 200 200 200 200 200 200 200 200 200 100 100 100 200 200 200 200 200 100 200 200 200 200 100 200 200 100 100 200 100 100 100 200 100 100 100 200 100 200 100 200 200 200 100 100 200 100 200 100 200 100 100 100 200 100 200 100 100 100 100 200 200 200 200 100 200 200 100 200 100 100 100 200 100 100 100 100 100 200 100 100 100 200 200 200 100 200 100 100 100 200 200",
"output": "YES"
},
{
"input": "99\n100 200 200 200 100 200 100 200 200 100 100 100 100 200 100 100 200 100 200 100 100 200 100 100 200 200 100 100 100 100 200 200 200 200 200 100 100 200 200 100 100 100 100 200 200 100 100 100 100 100 200 200 200 100 100 100 200 200 200 100 200 100 100 100 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 100 200 100 200 200 200 200 100 200 100 100 100 100 100 100 100 100 100",
"output": "YES"
},
{
"input": "99\n100 200 100 100 100 100 200 200 100 200 100 100 200 100 100 100 100 100 100 200 100 100 100 100 100 100 100 200 100 200 100 100 100 100 100 100 100 200 200 200 200 200 200 200 100 200 100 200 100 200 100 200 100 100 200 200 200 100 200 200 200 200 100 200 100 200 200 200 200 100 200 100 200 200 100 200 200 200 200 200 100 100 200 100 100 100 100 200 200 200 100 100 200 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "99\n200 100 100 100 200 200 200 100 100 100 100 100 100 100 100 100 200 200 100 200 200 100 200 100 100 200 200 200 100 200 100 200 200 100 200 100 200 200 200 100 100 200 200 200 200 100 100 100 100 200 200 200 200 100 200 200 200 100 100 100 200 200 200 100 200 100 200 100 100 100 200 100 200 200 100 200 200 200 100 100 100 200 200 200 100 200 200 200 100 100 100 200 100 200 100 100 100 200 200",
"output": "YES"
},
{
"input": "56\n100 200 200 200 200 200 100 200 100 100 200 100 100 100 100 100 200 200 200 100 200 100 100 200 200 200 100 200 100 200 200 100 100 100 100 100 200 100 200 100 200 200 200 100 100 200 200 200 200 200 200 200 200 200 200 100",
"output": "YES"
},
{
"input": "72\n200 100 200 200 200 100 100 200 200 100 100 100 100 200 100 200 100 100 100 100 200 100 200 100 100 200 100 100 200 100 200 100 100 200 100 200 100 100 200 200 200 200 200 100 100 200 200 200 200 100 100 100 200 200 100 100 100 100 100 200 100 100 200 100 100 200 200 100 100 200 100 200",
"output": "YES"
},
{
"input": "32\n200 200 200 100 100 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 200 200 200 200 200",
"output": "YES"
},
{
"input": "48\n200 200 200 200 200 200 100 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 100 200 100 200 200 200 200 200 200",
"output": "NO"
},
{
"input": "60\n100 100 200 200 100 200 100 200 100 100 100 100 100 100 200 100 100 100 200 100 200 100 100 100 100 100 200 100 200 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 200 100 100 100",
"output": "YES"
},
{
"input": "24\n200 200 100 100 200 100 200 200 100 200 200 200 200 200 100 200 200 200 200 200 200 200 200 100",
"output": "YES"
},
{
"input": "40\n100 100 200 200 200 200 100 100 100 200 100 100 200 200 100 100 100 100 100 200 100 200 200 100 200 200 200 100 100 100 100 100 200 200 100 200 100 100 200 100",
"output": "NO"
},
{
"input": "5\n200 200 200 200 200",
"output": "NO"
},
{
"input": "9\n100 100 100 200 100 100 200 100 200",
"output": "YES"
},
{
"input": "1\n200",
"output": "NO"
},
{
"input": "7\n200 200 200 100 200 200 200",
"output": "NO"
},
{
"input": "4\n100 100 200 200",
"output": "YES"
},
{
"input": "6\n100 100 100 200 200 200",
"output": "NO"
},
{
"input": "4\n200 100 100 200",
"output": "YES"
},
{
"input": "5\n100 100 100 100 200",
"output": "YES"
}
] | 30 | 0 | 0 | 1,684 |
|
954 | Castle Defense | [
"binary search",
"data structures",
"greedy",
"two pointers"
] | null | null | Today you are going to lead a group of elven archers to defend the castle that is attacked by an army of angry orcs. Three sides of the castle are protected by impassable mountains and the remaining side is occupied by a long wall that is split into *n* sections. At this moment there are exactly *a**i* archers located at the *i*-th section of this wall. You know that archer who stands at section *i* can shoot orcs that attack section located at distance not exceeding *r*, that is all such sections *j* that |*i*<=-<=*j*|<=β€<=*r*. In particular, *r*<==<=0 means that archers are only capable of shooting at orcs who attack section *i*.
Denote as defense level of section *i* the total number of archers who can shoot at the orcs attacking this section. Reliability of the defense plan is the minimum value of defense level of individual wall section.
There is a little time left till the attack so you can't redistribute archers that are already located at the wall. However, there is a reserve of *k* archers that you can distribute among wall sections in arbitrary way. You would like to achieve maximum possible reliability of the defence plan. | The first line of the input contains three integers *n*, *r* and *k* (1<=β€<=*n*<=β€<=500<=000, 0<=β€<=*r*<=β€<=*n*, 0<=β€<=*k*<=β€<=1018)Β β the number of sections of the wall, the maximum distance to other section archers can still shoot and the number of archers yet to be distributed along the wall. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109)Β β the current number of archers at each section. | Print one integerΒ β the maximum possible value of defense plan reliability, i.e. the maximum possible value of minimum defense level if we distribute *k* additional archers optimally. | [
"5 0 6\n5 4 3 4 9\n",
"4 2 0\n1 2 3 4\n",
"5 1 1\n2 1 2 1 2\n"
] | [
"5\n",
"6\n",
"3\n"
] | none | [
{
"input": "5 0 6\n5 4 3 4 9",
"output": "5"
},
{
"input": "4 2 0\n1 2 3 4",
"output": "6"
},
{
"input": "5 1 1\n2 1 2 1 2",
"output": "3"
},
{
"input": "1 0 0\n0",
"output": "0"
},
{
"input": "1 0 1000000000000000000\n1000000000",
"output": "1000000001000000000"
},
{
"input": "1 1 10\n23",
"output": "33"
},
{
"input": "2 0 100\n98 2",
"output": "100"
},
{
"input": "3 1 1000\n0 0 0",
"output": "1000"
},
{
"input": "100 0 0\n607 169 477 518 368 54 15 471 604 138 982 836 563 886 466 118 366 670 257 37 868 142 14 666 670 624 427 281 995 133 914 413 356 852 63 894 430 310 634 253 281 938 975 272 821 247 626 294 25 829 919 340 728 461 864 756 157 798 474 513 760 107 519 102 69 519 443 65 402 133 752 446 204 509 816 313 900 669 86 400 186 565 951 101 396 822 130 153 980 848 910 219 876 581 35 756 513 314 804 887",
"output": "14"
},
{
"input": "100 1 0\n724 108 796 141 159 89 957 672 396 460 791 468 147 431 700 934 298 95 131 53 676 96 361 438 413 23 171 529 258 834 612 398 120 429 296 231 273 159 936 623 85 72 598 550 205 345 1 414 772 912 464 953 909 883 524 46 542 898 984 859 432 551 972 763 310 233 399 358 875 660 992 946 883 96 20 626 663 234 896 233 790 644 6 563 341 201 980 391 619 508 610 534 698 110 468 934 320 331 700 638",
"output": "279"
},
{
"input": "100 3 0\n483 655 856 10 354 917 853 843 968 309 819 150 532 993 693 498 124 303 121 186 938 587 184 859 440 665 314 751 346 999 593 396 48 119 327 902 540 107 722 641 263 853 113 209 832 379 250 501 395 379 928 57 260 589 156 763 254 591 395 356 459 996 720 544 49 59 149 442 188 628 438 774 639 474 355 669 739 957 757 906 692 27 464 256 932 39 278 866 866 515 793 151 110 991 725 989 96 21 931 653",
"output": "1701"
},
{
"input": "100 100 0\n739 293 477 291 132 717 231 695 659 586 352 224 246 945 698 827 972 959 348 183 809 169 781 847 543 990 150 69 807 926 460 704 248 697 825 63 69 168 256 571 353 903 43 659 290 422 426 390 132 242 514 696 728 1 423 927 360 517 774 816 24 64 804 441 957 288 112 790 239 919 216 885 527 381 749 488 836 36 668 500 558 694 493 836 191 903 192 603 51 589 552 752 812 192 358 111 956 73 761 547",
"output": "50490"
},
{
"input": "100 0 1000\n328 12 814 345 865 163 256 233 778 3 425 264 632 323 78 747 566 591 696 729 499 649 643 553 732 730 758 194 338 694 407 506 218 408 242 979 924 70 298 59 899 252 38 438 577 594 498 702 355 492 851 712 900 25 34 890 215 360 536 797 418 864 739 823 853 363 734 224 874 537 471 422 592 460 963 589 563 905 437 426 411 948 968 417 31 828 917 829 591 886 429 512 160 252 982 921 276 285 155 422",
"output": "150"
},
{
"input": "100 1 1000\n923 286 937 954 258 701 953 262 346 809 109 678 963 496 245 110 75 633 900 434 608 144 574 425 646 458 188 991 341 293 121 513 548 323 139 5 360 95 951 725 107 727 892 139 493 206 433 390 733 568 629 525 303 570 539 151 266 273 459 604 724 682 411 187 763 624 610 596 383 447 376 671 479 815 2 192 270 245 187 790 159 359 561 541 66 882 809 595 980 421 753 795 551 331 296 872 929 77 207 447",
"output": "739"
},
{
"input": "100 3 1000\n391 145 605 150 641 788 1000 471 328 15 753 161 472 311 963 398 687 81 309 316 87 346 798 59 411 608 437 697 665 264 673 546 10 688 749 302 824 661 693 357 870 560 874 265 185 628 402 534 1000 649 551 412 384 273 249 324 54 204 946 811 478 106 616 981 219 280 432 838 149 155 385 3 883 81 892 70 736 137 309 787 359 405 927 934 211 828 587 896 722 34 936 355 958 541 308 463 404 709 654 248",
"output": "2050"
},
{
"input": "100 100 1000\n814 165 709 628 263 653 940 749 331 437 129 547 575 73 480 429 396 688 489 312 425 946 444 228 547 512 477 849 53 241 643 416 141 502 449 58 686 111 171 844 113 790 79 107 399 846 945 427 361 68 295 630 260 939 679 458 485 738 202 141 166 940 5 73 108 351 245 998 640 772 604 123 389 758 491 340 185 183 719 975 136 796 124 4 346 884 810 103 69 522 104 451 443 239 171 22 346 227 525 807",
"output": "44706"
},
{
"input": "1 0 0\n1",
"output": "1"
},
{
"input": "2 0 1\n1 1",
"output": "1"
}
] | 46 | 0 | 0 | 1,698 |
|
449 | Jzzhu and Cities | [
"graphs",
"greedy",
"shortest paths"
] | null | null | Jzzhu is the president of country A. There are *n* cities numbered from 1 to *n* in his country. City 1 is the capital of A. Also there are *m* roads connecting the cities. One can go from city *u**i* to *v**i* (and vise versa) using the *i*-th road, the length of this road is *x**i*. Finally, there are *k* train routes in the country. One can use the *i*-th train route to go from capital of the country to city *s**i* (and vise versa), the length of this route is *y**i*.
Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change. | The first line contains three integers *n*,<=*m*,<=*k* (2<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=3Β·105;Β 1<=β€<=*k*<=β€<=105).
Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*x**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*;Β *u**i*<=β <=*v**i*;Β 1<=β€<=*x**i*<=β€<=109).
Each of the next *k* lines contains two integers *s**i* and *y**i* (2<=β€<=*s**i*<=β€<=*n*;Β 1<=β€<=*y**i*<=β€<=109).
It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital. | Output a single integer representing the maximum number of the train routes which can be closed. | [
"5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5\n",
"2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3\n"
] | [
"2\n",
"2\n"
] | none | [
{
"input": "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5",
"output": "2"
},
{
"input": "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3",
"output": "2"
},
{
"input": "5 4 3\n1 2 999999999\n2 3 1000000000\n3 4 529529529\n5 1 524524524\n5 524444444\n5 529999999\n2 1000000000",
"output": "2"
},
{
"input": "3 2 5\n1 2 2\n2 3 4\n3 5\n3 5\n3 5\n3 6\n3 7",
"output": "4"
},
{
"input": "5 5 3\n1 2 999999999\n2 3 1000000000\n3 4 529529529\n5 1 524524524\n5 3 1000000000\n5 524444444\n5 529999999\n2 1000000000",
"output": "2"
},
{
"input": "2 1 5\n1 2 4\n2 3\n2 5\n2 4\n2 4\n2 5",
"output": "4"
},
{
"input": "3 3 6\n1 2 499999999\n2 3 500000000\n1 3 999999999\n2 499999999\n2 500000000\n2 499999999\n3 999999999\n3 1000000000\n3 1000000000",
"output": "6"
},
{
"input": "2 1 1\n1 2 1\n2 1000000000",
"output": "1"
},
{
"input": "3 2 2\n1 2 4\n2 3 4\n2 2\n3 6",
"output": "1"
},
{
"input": "5 5 2\n1 2 100\n2 3 100\n3 4 100\n4 5 20\n2 5 5\n5 50\n4 1",
"output": "1"
},
{
"input": "3 2 2\n1 2 100\n2 3 1\n2 1\n3 3",
"output": "1"
}
] | 1,123 | 87,961,600 | 0 | 1,699 |
|
440 | Forgotten Episode | [
"implementation"
] | null | null | Polycarpus adores TV series. Right now he is ready to finish watching a season of a popular sitcom "Graph Theory". In total, the season has *n* episodes, numbered with integers from 1 to *n*.
Polycarpus watches episodes not one by one but in a random order. He has already watched all the episodes except for one. Which episode has Polycaprus forgotten to watch? | The first line of the input contains integer *n* (2<=β€<=*n*<=β€<=100000)Β β the number of episodes in a season. Assume that the episodes are numbered by integers from 1 to *n*.
The second line contains *n*<=-<=1 integer *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*)Β β the numbers of episodes that Polycarpus has watched. All values of *a**i* are distinct. | Print the number of the episode that Polycarpus hasn't watched. | [
"10\n3 8 10 1 7 9 6 5 2\n"
] | [
"4\n"
] | none | [
{
"input": "10\n3 8 10 1 7 9 6 5 2",
"output": "4"
},
{
"input": "5\n4 3 2 1",
"output": "5"
},
{
"input": "2\n1",
"output": "2"
},
{
"input": "2\n2",
"output": "1"
},
{
"input": "3\n1 2",
"output": "3"
},
{
"input": "3\n1 3",
"output": "2"
},
{
"input": "3\n2 3",
"output": "1"
},
{
"input": "3\n2 1",
"output": "3"
},
{
"input": "3\n3 1",
"output": "2"
},
{
"input": "3\n3 2",
"output": "1"
},
{
"input": "5\n2 3 4 5",
"output": "1"
},
{
"input": "5\n5 4 3 2",
"output": "1"
},
{
"input": "5\n5 2 4 3",
"output": "1"
},
{
"input": "5\n1 2 3 4",
"output": "5"
},
{
"input": "5\n2 1 3 4",
"output": "5"
},
{
"input": "5\n1 5 3 4",
"output": "2"
},
{
"input": "5\n1 4 5 2",
"output": "3"
},
{
"input": "5\n2 1 5 3",
"output": "4"
},
{
"input": "5\n2 3 4 5",
"output": "1"
}
] | 31 | 0 | 0 | 1,701 |
|
825 | Multi-judge Solving | [
"greedy",
"implementation"
] | null | null | Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty β a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty *d* on Decoforces is as hard as the problem with difficulty *d* on any other judge).
Makes has chosen *n* problems to solve on Decoforces with difficulties *a*1,<=*a*2,<=...,<=*a**n*. He can solve these problems in arbitrary order. Though he can solve problem *i* with difficulty *a**i* only if he had already solved some problem with difficulty (no matter on what online judge was it).
Before starting this chosen list of problems, Makes has already solved problems with maximum difficulty *k*.
With given conditions it's easy to see that Makes sometimes can't solve all the chosen problems, no matter what order he chooses. So he wants to solve some problems on other judges to finish solving problems from his list.
For every positive integer *y* there exist some problem with difficulty *y* on at least one judge besides Decoforces.
Makes can solve problems on any judge at any time, it isn't necessary to do problems from the chosen list one right after another.
Makes doesn't have too much free time, so he asked you to calculate the minimum number of problems he should solve on other judges in order to solve all the chosen problems from Decoforces. | The first line contains two integer numbers *n*, *k* (1<=β€<=*n*<=β€<=103, 1<=β€<=*k*<=β€<=109).
The second line contains *n* space-separated integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). | Print minimum number of problems Makes should solve on other judges in order to solve all chosen problems on Decoforces. | [
"3 3\n2 1 9\n",
"4 20\n10 3 6 3\n"
] | [
"1\n",
"0\n"
] | In the first example Makes at first solves problems 1 and 2. Then in order to solve the problem with difficulty 9, he should solve problem with difficulty no less than 5. The only available are difficulties 5 and 6 on some other judge. Solving any of these will give Makes opportunity to solve problem 3.
In the second example he can solve every problem right from the start. | [
{
"input": "3 3\n2 1 9",
"output": "1"
},
{
"input": "4 20\n10 3 6 3",
"output": "0"
},
{
"input": "1 1000000000\n1",
"output": "0"
},
{
"input": "1 1\n3",
"output": "1"
},
{
"input": "50 100\n74 55 33 5 83 24 75 59 30 36 13 4 62 28 96 17 6 35 45 53 33 11 37 93 34 79 61 72 13 31 44 75 7 3 63 46 18 16 44 89 62 25 32 12 38 55 75 56 61 82",
"output": "0"
},
{
"input": "100 10\n246 286 693 607 87 612 909 312 621 37 801 558 504 914 416 762 187 974 976 123 635 488 416 659 988 998 93 662 92 749 889 78 214 786 735 625 921 372 713 617 975 119 402 411 878 138 548 905 802 762 940 336 529 373 745 835 805 880 816 94 166 114 475 699 974 462 61 337 555 805 968 815 392 746 591 558 740 380 668 29 881 151 387 986 174 923 541 520 998 947 535 651 103 584 664 854 180 852 726 93",
"output": "1"
},
{
"input": "2 1\n1 1000000000",
"output": "29"
},
{
"input": "29 2\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",
"output": "27"
},
{
"input": "1 1\n1000000000",
"output": "29"
},
{
"input": "7 6\n4 20 16 14 3 17 4",
"output": "1"
},
{
"input": "2 1\n3 6",
"output": "1"
},
{
"input": "1 1\n20",
"output": "4"
},
{
"input": "5 2\n86 81 53 25 18",
"output": "4"
},
{
"input": "4 1\n88 55 14 39",
"output": "4"
},
{
"input": "3 1\n2 3 6",
"output": "0"
},
{
"input": "3 2\n4 9 18",
"output": "1"
},
{
"input": "5 3\n6 6 6 13 27",
"output": "2"
},
{
"input": "5 1\n23 8 83 26 18",
"output": "4"
},
{
"input": "3 1\n4 5 6",
"output": "1"
},
{
"input": "3 1\n1 3 6",
"output": "1"
},
{
"input": "1 1\n2",
"output": "0"
},
{
"input": "3 2\n4 5 6",
"output": "0"
},
{
"input": "5 1\n100 200 400 1000 2000",
"output": "7"
},
{
"input": "2 1\n1 4",
"output": "1"
},
{
"input": "4 1\n2 4 8 32",
"output": "1"
},
{
"input": "2 10\n21 42",
"output": "1"
},
{
"input": "3 3\n1 7 13",
"output": "1"
},
{
"input": "3 1\n1 4 6",
"output": "1"
},
{
"input": "2 2\n2 8",
"output": "1"
},
{
"input": "1 1\n4",
"output": "1"
},
{
"input": "2 2\n8 16",
"output": "1"
},
{
"input": "3 1\n4 8 16",
"output": "1"
},
{
"input": "3 1\n3 6 9",
"output": "1"
},
{
"input": "2 1\n4 8",
"output": "1"
},
{
"input": "2 2\n7 14",
"output": "1"
},
{
"input": "1 4\n9",
"output": "1"
},
{
"input": "5 3\n1024 4096 16384 65536 536870913",
"output": "24"
},
{
"input": "2 5\n10 11",
"output": "0"
},
{
"input": "2 2\n3 6",
"output": "0"
},
{
"input": "2 2\n8 11",
"output": "1"
},
{
"input": "3 19905705\n263637263 417905394 108361057",
"output": "3"
},
{
"input": "4 25\n100 11 1 13",
"output": "1"
},
{
"input": "10 295206008\n67980321 440051990 883040288 135744260 96431758 242465794 576630162 972797687 356406646 547451696",
"output": "0"
},
{
"input": "4 2\n45 44 35 38",
"output": "4"
},
{
"input": "1 2\n9",
"output": "2"
},
{
"input": "3 6\n13 26 52",
"output": "1"
},
{
"input": "9 30111088\n824713578 11195876 458715185 731769293 680826358 189542586 550198537 860586039 101083021",
"output": "2"
},
{
"input": "3 72014068\n430005292 807436976 828082746",
"output": "2"
},
{
"input": "3 165219745\n737649884 652879952 506420386",
"output": "1"
},
{
"input": "2 60669400\n95037700 337255240",
"output": "1"
},
{
"input": "4 28\n34 1 86 90",
"output": "1"
},
{
"input": "2 1\n5 10",
"output": "2"
},
{
"input": "2 1\n4 1000000000",
"output": "28"
},
{
"input": "2 1\n2 3",
"output": "0"
},
{
"input": "2 1\n3 5",
"output": "1"
},
{
"input": "3 3\n1 5 20",
"output": "1"
},
{
"input": "9 1\n1 2 4 9 15 32 64 128 1024",
"output": "4"
}
] | 62 | 4,608,000 | 0 | 1,702 |
|
171 | Mysterious numbers - 1 | [
"*special",
"constructive algorithms"
] | null | null | The input contains two integers *a*1,<=*a*2 (0<=β€<=*a**i*<=β€<=109), separated by a single space.
Output a single integer. | The input contains two integers *a*1,<=*a*2 (0<=β€<=*a**i*<=β€<=109), separated by a single space. | Output a single integer. | [
"3 14\n",
"27 12\n",
"100 200\n"
] | [
"44\n",
"48\n",
"102\n"
] | none | [
{
"input": "3 14",
"output": "44"
},
{
"input": "27 12",
"output": "48"
},
{
"input": "100 200",
"output": "102"
},
{
"input": "0 0",
"output": "0"
},
{
"input": "31415 92653",
"output": "67044"
},
{
"input": "1000000000 0",
"output": "1000000000"
},
{
"input": "0 1000000000",
"output": "1"
},
{
"input": "1000000000 1000000000",
"output": "1000000001"
},
{
"input": "804289383 846930886",
"output": "1492329031"
},
{
"input": "681692777 714636915",
"output": "1201329194"
},
{
"input": "957747793 424238335",
"output": "1491580217"
},
{
"input": "719885386 649760492",
"output": "1013953332"
},
{
"input": "596516649 189641421",
"output": "720663630"
},
{
"input": "25202362 350490027",
"output": "745296415"
},
{
"input": "783368690 102520059",
"output": "1733393891"
},
{
"input": "44897763 967513926",
"output": "674213532"
},
{
"input": "365180540 540383426",
"output": "989563585"
},
{
"input": "304089172 303455736",
"output": "941643475"
},
{
"input": "35005211 521595368",
"output": "898600336"
},
{
"input": "294702567 726956429",
"output": "1219362194"
},
{
"input": "336465782 861021530",
"output": "371585950"
},
{
"input": "278722862 233665123",
"output": "600289194"
},
{
"input": "145174067 468703135",
"output": "676481931"
},
{
"input": "101513929 801979802",
"output": "310493037"
},
{
"input": "315634022 635723058",
"output": "1165961558"
},
{
"input": "369133069 125898167",
"output": "1131031590"
},
{
"input": "59961393 89018456",
"output": "125442491"
},
{
"input": "628175011 656478042",
"output": "869049667"
},
{
"input": "131176229 653377373",
"output": "504949585"
},
{
"input": "859484421 914544919",
"output": "1778929840"
},
{
"input": "608413784 756898537",
"output": "1344312441"
},
{
"input": "734575198 973594324",
"output": "1158070577"
},
{
"input": "149798315 38664370",
"output": "157144998"
},
{
"input": "129566413 184803526",
"output": "754874894"
},
{
"input": "412776091 424268980",
"output": "502638515"
},
{
"input": "911759956 749241873",
"output": "1289902903"
},
{
"input": "137806862 42999170",
"output": "145006786"
},
{
"input": "982906996 135497281",
"output": "1165701527"
},
{
"input": "511702305 84420925",
"output": "564604753"
},
{
"input": "937477084 827336327",
"output": "1661110812"
},
{
"input": "1000000000 999999999",
"output": "1999999999"
}
] | 0 | 0 | -1 | 1,706 |
|
899 | Shovel Sale | [
"constructive algorithms",
"math"
] | null | null | There are *n* shovels in Polycarp's shop. The *i*-th shovel costs *i* burles, that is, the first shovel costs 1 burle, the second shovel costs 2 burles, the third shovel costs 3 burles, and so on. Polycarps wants to sell shovels in pairs.
Visitors are more likely to buy a pair of shovels if their total cost ends with several 9s. Because of this, Polycarp wants to choose a pair of shovels to sell in such a way that the sum of their costs ends with maximum possible number of nines. For example, if he chooses shovels with costs 12345 and 37454, their total cost is 49799, it ends with two nines.
You are to compute the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Two pairs are considered different if there is a shovel presented in one pair, but not in the other. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=109) β the number of shovels in Polycarp's shop. | Print the number of pairs of shovels such that their total cost ends with maximum possible number of nines.
Note that it is possible that the largest number of 9s at the end is 0, then you should count all such ways.
It is guaranteed that for every *n*<=β€<=109 the answer doesn't exceed 2Β·109. | [
"7\n",
"14\n",
"50\n"
] | [
"3\n",
"9\n",
"1\n"
] | In the first example the maximum possible number of nines at the end is one. Polycarp cah choose the following pairs of shovels for that purpose:
- 2 and 7; - 3 and 6; - 4 and 5.
In the second example the maximum number of nines at the end of total cost of two shovels is one. The following pairs of shovels suit Polycarp:
- 1 and 8; - 2 and 7; - 3 and 6; - 4 and 5; - 5 and 14; - 6 and 13; - 7 and 12; - 8 and 11; - 9 and 10.
In the third example it is necessary to choose shovels 49 and 50, because the sum of their cost is 99, that means that the total number of nines is equal to two, which is maximum possible for *n*β=β50. | [
{
"input": "7",
"output": "3"
},
{
"input": "14",
"output": "9"
},
{
"input": "50",
"output": "1"
},
{
"input": "999999999",
"output": "499999999"
},
{
"input": "15",
"output": "11"
},
{
"input": "3",
"output": "3"
},
{
"input": "6500",
"output": "1501"
},
{
"input": "4",
"output": "6"
},
{
"input": "13",
"output": "8"
},
{
"input": "10",
"output": "5"
},
{
"input": "499999",
"output": "1249995"
},
{
"input": "6",
"output": "2"
},
{
"input": "8",
"output": "4"
},
{
"input": "9",
"output": "4"
},
{
"input": "11",
"output": "6"
},
{
"input": "12",
"output": "7"
},
{
"input": "5",
"output": "1"
},
{
"input": "16",
"output": "13"
},
{
"input": "17",
"output": "15"
},
{
"input": "18",
"output": "17"
},
{
"input": "19",
"output": "18"
},
{
"input": "20",
"output": "20"
},
{
"input": "21",
"output": "22"
},
{
"input": "22",
"output": "24"
},
{
"input": "23",
"output": "26"
},
{
"input": "24",
"output": "28"
},
{
"input": "25",
"output": "31"
},
{
"input": "26",
"output": "34"
},
{
"input": "27",
"output": "37"
},
{
"input": "28",
"output": "40"
},
{
"input": "29",
"output": "42"
},
{
"input": "30",
"output": "45"
},
{
"input": "31",
"output": "48"
},
{
"input": "32",
"output": "51"
},
{
"input": "33",
"output": "54"
},
{
"input": "34",
"output": "57"
},
{
"input": "35",
"output": "61"
},
{
"input": "36",
"output": "65"
},
{
"input": "37",
"output": "69"
},
{
"input": "38",
"output": "73"
},
{
"input": "39",
"output": "76"
},
{
"input": "40",
"output": "80"
},
{
"input": "41",
"output": "84"
},
{
"input": "42",
"output": "88"
},
{
"input": "43",
"output": "92"
},
{
"input": "44",
"output": "96"
},
{
"input": "45",
"output": "101"
},
{
"input": "46",
"output": "106"
},
{
"input": "47",
"output": "111"
},
{
"input": "48",
"output": "116"
},
{
"input": "49",
"output": "120"
},
{
"input": "51",
"output": "2"
},
{
"input": "100",
"output": "50"
},
{
"input": "99",
"output": "49"
},
{
"input": "101",
"output": "51"
},
{
"input": "4999",
"output": "12495"
},
{
"input": "4998",
"output": "12491"
},
{
"input": "4992",
"output": "12461"
},
{
"input": "5000",
"output": "1"
},
{
"input": "5001",
"output": "2"
},
{
"input": "10000",
"output": "5000"
},
{
"input": "10001",
"output": "5001"
},
{
"input": "49839",
"output": "124196"
},
{
"input": "4999999",
"output": "12499995"
},
{
"input": "49999999",
"output": "124999995"
},
{
"input": "499999999",
"output": "1249999995"
},
{
"input": "999",
"output": "499"
},
{
"input": "9999",
"output": "4999"
},
{
"input": "99999",
"output": "49999"
},
{
"input": "999999",
"output": "499999"
},
{
"input": "9999999",
"output": "4999999"
},
{
"input": "99999999",
"output": "49999999"
},
{
"input": "2",
"output": "1"
},
{
"input": "1000000000",
"output": "500000000"
},
{
"input": "764675465",
"output": "264675466"
},
{
"input": "499999998",
"output": "1249999991"
},
{
"input": "167959139",
"output": "135918279"
},
{
"input": "641009859",
"output": "141009860"
},
{
"input": "524125987",
"output": "24125988"
},
{
"input": "702209411",
"output": "202209412"
},
{
"input": "585325539",
"output": "85325540"
},
{
"input": "58376259",
"output": "8376260"
},
{
"input": "941492387",
"output": "441492388"
},
{
"input": "824608515",
"output": "324608516"
},
{
"input": "2691939",
"output": "3575818"
},
{
"input": "802030518",
"output": "302030519"
},
{
"input": "685146646",
"output": "185146647"
},
{
"input": "863230070",
"output": "363230071"
},
{
"input": "41313494",
"output": "85253976"
},
{
"input": "219396918",
"output": "238793836"
},
{
"input": "102513046",
"output": "52513046"
},
{
"input": "985629174",
"output": "485629175"
},
{
"input": "458679894",
"output": "1043399471"
},
{
"input": "341796022",
"output": "575388066"
},
{
"input": "519879446",
"output": "19879447"
},
{
"input": "452405440",
"output": "1012027201"
},
{
"input": "335521569",
"output": "556564707"
},
{
"input": "808572289",
"output": "308572290"
},
{
"input": "691688417",
"output": "191688418"
},
{
"input": "869771841",
"output": "369771842"
},
{
"input": "752887969",
"output": "252887970"
},
{
"input": "930971393",
"output": "430971394"
},
{
"input": "109054817",
"output": "59054817"
},
{
"input": "992170945",
"output": "492170946"
},
{
"input": "170254369",
"output": "140508739"
},
{
"input": "248004555",
"output": "296009110"
}
] | 93 | 0 | 3 | 1,715 |
|
920 | List Of Integers | [
"binary search",
"bitmasks",
"brute force",
"combinatorics",
"math",
"number theory"
] | null | null | Let's denote as *L*(*x*,<=*p*) an infinite sequence of integers *y* such that *gcd*(*p*,<=*y*)<==<=1 and *y*<=><=*x* (where *gcd* is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of *L*(*x*,<=*p*) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of *L*(7,<=22), respectively.
You have to process *t* queries. Each query is denoted by three integers *x*, *p* and *k*, and the answer to this query is *k*-th element of *L*(*x*,<=*p*). | The first line contains one integer *t* (1<=β€<=*t*<=β€<=30000) β the number of queries to process.
Then *t* lines follow. *i*-th line contains three integers *x*, *p* and *k* for *i*-th query (1<=β€<=*x*,<=*p*,<=*k*<=β€<=106). | Print *t* integers, where *i*-th integer is the answer to *i*-th query. | [
"3\n7 22 1\n7 22 2\n7 22 3\n",
"5\n42 42 42\n43 43 43\n44 44 44\n45 45 45\n46 46 46\n"
] | [
"9\n13\n15\n",
"187\n87\n139\n128\n141\n"
] | none | [
{
"input": "3\n7 22 1\n7 22 2\n7 22 3",
"output": "9\n13\n15"
},
{
"input": "5\n42 42 42\n43 43 43\n44 44 44\n45 45 45\n46 46 46",
"output": "187\n87\n139\n128\n141"
}
] | 62 | 6,041,600 | -1 | 1,716 |
|
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"
}
] | 124 | 0 | 0 | 1,718 |
|
725 | Contest Balloons | [
"data structures",
"greedy"
] | null | null | One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed.
You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings.
A contest has just finished. There are *n* teams, numbered 1 through *n*. The *i*-th team has *t**i* balloons and weight *w**i*. It's guaranteed that *t**i* doesn't exceed *w**i* so nobody floats initially.
Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has.
What is the best place Limak can get? | The first line of the standard input contains one integer *n* (2<=β€<=*n*<=β€<=300<=000)Β β the number of teams.
The *i*-th of *n* following lines contains two integers *t**i* and *w**i* (0<=β€<=*t**i*<=β€<=*w**i*<=β€<=1018)Β β respectively the number of balloons and the weight of the *i*-th team. Limak is a member of the first team. | Print one integer denoting the best place Limak can get. | [
"8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000\n",
"7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5\n",
"7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n46000000000 51000000000\n0 1000000000\n0 0\n"
] | [
"3\n",
"2\n",
"2\n"
] | In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is:
1. Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place.1. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified.1. Limak gives 1 balloon to each of two teams with 16 balloons initially.1. Limak has 20β-β6β-β6β-β1β-β1β=β6 balloons.1. There are three other teams left and their numbers of balloons are 40, 14 and 2.1. Limak gets the third place because there are two teams with more balloons.
In the second sample, Limak has the second place and he can't improve it.
In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81β000β000β000, 5β000β000β000 and 46β000β000β000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7). | [
{
"input": "8\n20 1000\n32 37\n40 1000\n45 50\n16 16\n16 16\n14 1000\n2 1000",
"output": "3"
},
{
"input": "7\n4 4\n4 4\n4 4\n4 4\n4 4\n4 4\n5 5",
"output": "2"
},
{
"input": "7\n14000000003 1000000000000000000\n81000000000 88000000000\n5000000000 7000000000\n15000000000 39000000000\n46000000000 51000000000\n0 1000000000\n0 0",
"output": "2"
},
{
"input": "2\n100 150\n5 100000",
"output": "1"
},
{
"input": "9\n4 70\n32 56\n32 65\n77 78\n5 29\n72 100\n0 55\n42 52\n66 72",
"output": "7"
},
{
"input": "3\n1 2\n12 19\n25 45",
"output": "3"
},
{
"input": "5\n2 23\n1 13\n3 9\n0 20\n6 7",
"output": "3"
},
{
"input": "10\n19 22\n10 77\n3 52\n16 42\n25 67\n14 42\n44 85\n37 39\n36 62\n6 85",
"output": "4"
},
{
"input": "15\n143 698\n269 879\n100 728\n86 855\n368 478\n174 368\n442 980\n812 825\n121 220\n137 198\n599 706\n423 586\n96 647\n177 439\n54 620",
"output": "9"
},
{
"input": "3\n1000 1000\n1001 1001\n700 1000000",
"output": "1"
},
{
"input": "5\n4 100\n10 11\n10 11\n3 3\n3 3",
"output": "2"
}
] | 46 | 0 | 0 | 1,721 |
|
989 | A Mist of Florescence | [
"constructive algorithms",
"graphs"
] | null | null | "I've been here once," Mino exclaims with delight, "it's breathtakingly amazing."
"What is it like?"
"Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?"
There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.
The wood can be represented by a rectangular grid of $n$ rows and $m$ columns. In each cell of the grid, there is exactly one type of flowers.
According to Mino, the numbers of connected components formed by each kind of flowers are $a$, $b$, $c$ and $d$ respectively. Two cells are considered in the same connected component if and only if a path exists between them that moves between cells sharing common edges and passes only through cells containing the same flowers.
You are to help Kanno depict such a grid of flowers, with $n$ and $m$ arbitrarily chosen under the constraints given below. It can be shown that at least one solution exists under the constraints of this problem.
Note that you can choose arbitrary $n$ and $m$ under the constraints below, they are not given in the input. | The first and only line of input contains four space-separated integers $a$, $b$, $c$ and $d$ ($1 \leq a, b, c, d \leq 100$)Β β the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively. | In the first line, output two space-separated integers $n$ and $m$ ($1 \leq n, m \leq 50$)Β β the number of rows and the number of columns in the grid respectively.
Then output $n$ lines each consisting of $m$ consecutive English letters, representing one row of the grid. Each letter should be among 'A', 'B', 'C' and 'D', representing Amaranths, Begonias, Centaureas and Dianthuses, respectively.
In case there are multiple solutions, print any. You can output each letter in either case (upper or lower). | [
"5 3 2 1\n",
"50 50 1 1\n",
"1 6 4 5\n"
] | [
"4 7\nDDDDDDD\nDABACAD\nDBABACD\nDDDDDDD",
"4 50\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nABABABABABABABABABABABABABABABABABABABABABABABABAB\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
"7 7\nDDDDDDD\nDDDBDBD\nDDCDCDD\nDBDADBD\nDDCDCDD\nDBDBDDD\nDDDDDDD"
] | In the first example, each cell of Amaranths, Begonias and Centaureas forms a connected component, while all the Dianthuses form one. | [
{
"input": "5 3 2 1",
"output": "5 13\nAABABBBBCDDAD\nABAABBBBCDADD\nAAAABBBBCDDAD\nAAAABCBBCDADD\nAAAABBBBCDDDD"
},
{
"input": "50 50 1 1",
"output": "10 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\nDADADADADADADADADADADADADADADADADADADADADADADADADD\nADADADADADADADADADADADADADADADADADADADADADADADADAD\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD..."
},
{
"input": "1 6 4 5",
"output": "6 13\nAABABBCBCCDCD\nABAABBBBCCCCD\nAABABBCBCCDCD\nABAABCBBCDCCD\nAABABBBBCCDCD\nAAAABBBBCCCCD"
},
{
"input": "1 1 1 1",
"output": "2 4\nABCD\nABCD"
},
{
"input": "4 8 16 32",
"output": "16 32\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABAAAAAAABAAAAAAAAAAAAAAABABAAAA\nBAAAAAAAAAAABAAAAAAAAAAABAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBBBBBCBCBCBCBCBBBCBCBBBBBBBBBB\nCBCBBBBBBBBBCBBBCBBBCBBBBBCBBBCB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCC\nDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\nADDDDDDDDDDDDDDDADDDDDDDDDDD..."
},
{
"input": "1 1 1 50",
"output": "7 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCC\nDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
},
{
"input": "19 58 20 18",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABAAABABABABABAAABABAAAAABABAAABABAAAAABAA\nAAAABABABAAABABABABABAAABAAABAAABAAAAAAABAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABAAABABABAAABABABABAAAAAAABABAAABAAABAAABAAABABA\nABABAAABAAABABAAABAAAAABAAABABAAABAAAAABAAABAAAAAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBBBBBCBBBCBCBBBBBBBBBCBBBCBBBBBBBBBBBCBCBB\nBBBBCBCBCBCBBBBBCBBBBBCBCBCBBBCBBBBB..."
},
{
"input": "100 100 100 100",
"output": "40 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAABAAABAAABAAABABABABABABAAABABABABABABAAAAABABAA\nAABAAAAAAAAAAABAAABAAABABABAAABABAAABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAABABABAAAAAAABABABABABAAABABABABABABAAAAAAABABA\nABABABAAABABABAAABABAAABAAABABABABABAAABABABABABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABAAABAAABABAAAAAAABAAAAAAABABAAABAAABAAAAABAA\nBABABABAAABABABABABAAABABABABAAABABAAABABABAAABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..."
},
{
"input": "1 1 1 2",
"output": "2 7\nABCCDCD\nABCCCCD"
},
{
"input": "1 1 3 1",
"output": "3 7\nABBCBCD\nABCBBCD\nABBBBCD"
},
{
"input": "1 4 1 1",
"output": "4 7\nAABABCD\nABAABCD\nAABABCD\nAAAABCD"
},
{
"input": "5 1 1 1",
"output": "5 7\nABCDDAD\nABCDADD\nABCDDAD\nABCDADD\nABCDDDD"
},
{
"input": "1 4 7 3",
"output": "7 13\nAAAABBCBCCDCD\nABAABCBBCCCCD\nAAAABBCBCCCCD\nABAABCBBCCCCD\nAABABBCBCCCCD\nAAAABCBBCDCCD\nAAAABBBBCCCCD"
},
{
"input": "6 2 5 1",
"output": "6 13\nAAAABBCBCDDAD\nAAAABBBBCDADD\nAAAABBCBCDDAD\nAAAABCBBCDADD\nAABABBCBCDDAD\nAAAABBBBCDDDD"
},
{
"input": "1 5 6 3",
"output": "6 13\nAAAABBCBCCCCD\nABAABCBBCCCCD\nAABABBCBCCCCD\nABAABCBBCDCCD\nAABABBCBCCDCD\nAAAABBBBCCCCD"
},
{
"input": "4 1 4 5",
"output": "5 13\nABBCBCCDCDDAD\nABCBBCDCCDDDD\nABBBBCCDCDDAD\nABCBBCDCCDADD\nABBBBCCCCDDDD"
},
{
"input": "4 5 3 6",
"output": "6 16\nAAAABBCBCCDCDDAD\nABAABBBBCDCCDDDD\nAABABBCBCCDCDDAD\nABAABBBBCDCCDDDD\nAABABBBBCCDCDDAD\nAAAABBBBCCCCDDDD"
},
{
"input": "2 5 1 17",
"output": "13 17\nAAAAAAAAAAAAAAAAA\nABAAAAAAABAAAAAAA\nAABAAAAAAAAABAAAA\nAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDC\nDCDCDCDCDCDCDCDCC\nCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDD\nDDADDDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDD"
},
{
"input": "11 4 5 14",
"output": "14 16\nAAAABBBBCCDCDDDD\nABAABBBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABBBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABBBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABCBBCDCCDDDD\nAAAABBCBCCDCDDDD\nABAABCBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABCBBCDCCDADD\nAABABBBBCCDCDDAD\nAAAABBBBCCCCDDDD"
},
{
"input": "19 19 8 10",
"output": "16 19\nAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABA\nBABABABABABABABABAA\nAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBBBBBCBBBCB\nBBCBBBCBCBBBBBCBBBB\nBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCC\nCCCDCCCCCDCCCDCCCDC\nCCCCDCDCCCDCCCDCDCC\nCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDD\nDADADADADADADADADAD\nADADADADADADADADADD\nDDDDDDDDDDDDDDDDDDD"
},
{
"input": "49 49 49 49",
"output": "16 49\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABA\nBABABABABABABABABABABABABABABABABABABABABABABABAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCB\nCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC..."
},
{
"input": "49 50 50 50",
"output": "16 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBB\nCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD..."
},
{
"input": "50 50 51 50",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBCBCBBBBBCBBBCBBBCBBBBBBBCBCBBBCBBBBBCBBBB\nCBCBCBCBCBBBBBBBBBBBBBBBBBBBCBCBBBCBCBCBBBCBCBCBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBCBCBBBBBCBBBCBBBCBCBCBCBCBCBCBCBCBCBBBCBBBBBCBCB\nBBBBBCBBBCBBBBBCBBBBBCBBBCBBBCBBBCBB..."
},
{
"input": "15 63 41 45",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABAAABABABABABAAABABAAAAABABAAABABAAAAABAA\nAAAABABABAAABABABABABAAABAAABAAABABABABABAAABAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABAAABABABAAABABABABAAAAAAABABAAABAAABAAABAAABABA\nABABAAABAAABABAAABAAAAABAAABABAAABABAAABAAABAAAAAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBCBCBCBCBCBCBCBCBCBBBCBBBCBBBCBCBCBCBCBCBCBCBB\nCBBBCBCBCBCBCBCBCBBBCBCBCBCBCBCBBBBB..."
},
{
"input": "45 36 25 13",
"output": "16 45\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABAAABABABABAAABABABABABAAABABABABABAAAAABAAA\nBABABABABABABAAAAABABABABABABABABABABAAABABAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBBBCBBBBBBBBBBBBBBBCBCBCBBBCBCBBBCB\nCBBBCBCBCBCBBBBBCBBBBBBBBBCBBBCBBBBBCBCBCBCBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCCCCCCCCCCCCCCCCCCCDCCCCCCCCCDCCCCCDCCCCCDCCC\nCCCCCCCCCCCCDCCCDCCCCCDCDCCCCCDCCC..."
},
{
"input": "31 41 59 26",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABAAAAABAAABABABABABABABABAAABABABABABAA\nBABABABABABABAAABABAAABAAABAAABABABABAAABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBCBCBCBBBCBBBCBBBCBBBBBBBCBCBBBCBBBBBCBBBB\nCBCBCBCBCBCBBBBBBBBBBBBBCBBBCBCBBBCBCBCBBBCBCBCBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBCBCBBBBBCBBBCBBBCBCBCBCBCBCBCBCBCBCBBBCBBBBBCBCB\nBCBBBCBBBCBBBCBCBBBBBCBBBCBBBCBCBCBC..."
},
{
"input": "18 90 64 16",
"output": "22 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABAAABAA\nAABABABABAAABABABABABABABAAABABABABABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABABABABABABABABABABABABABABABAAABABABABABABABABA\nABABABABABABABAAABABAAABABABABABABABABABABABAAABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBBBCBCBCBBBCBBBBBCBCBCBBBCBCBCBBBCBBBCBCBCBCBCBB\nCBCBCBBBCBCBBBCBBBCBCBBBCBCBBBCBCBBB..."
},
{
"input": "77 88 99 1",
"output": "22 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABAAAAABAA\nAABABABABAAABABABABABABABAAABABABABABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABABABABABABABABABABABABAAABABAAABABABABABABABABA\nABABABABABABABAAABABAAABABABABABABABABABABABAAABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBB\nCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCB..."
},
{
"input": "99 100 1 100",
"output": "28 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAABAAABAAABAAABABABABABABAAABABABABABABAAAAABABAA\nAABAAAAAAAAAAABAAABAAABABABAAABABAAABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAABABABAAAAAAABABABABABAAABABABABABABAAAAAAABABA\nABABABAAABABABAAABABAAABAAABABABABABAAABABABABABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABAAABAAABABAAAAAAABAAAAAAABABAAABAAABAAAAABAA\nBABABABAAABABABABABAAABABABABAAABABAAABABABAAABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..."
},
{
"input": "100 50 100 49",
"output": "28 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBBBBBCBCBCBBBCBBBBBCBCBBBCBCBCBBBCBCBCBCBBBBBB\nBBBBCBCBCBCBBBCBCBCBCBCBBBCBCBCBCBBBCBCBCBCBCBCBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBCBCBBBBBCBCBCBCBCBCBCBCBCBCBBBCBCBCBBBBBCBCBCBCB\nBCBBBBBCBCBCBBBCBCBBBCBCBCBBBBBCBCBB..."
}
] | 140 | 2,560,000 | 0 | 1,724 |
|
787 | The Monster | [
"brute force",
"math",
"number theory"
] | null | null | A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times *b*,<=*b*<=+<=*a*,<=*b*<=+<=2*a*,<=*b*<=+<=3*a*,<=... and Morty screams at times *d*,<=*d*<=+<=*c*,<=*d*<=+<=2*c*,<=*d*<=+<=3*c*,<=....
The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time. | The first line of input contains two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=100).
The second line contains two integers *c* and *d* (1<=β€<=*c*,<=*d*<=β€<=100). | Print the first time Rick and Morty will scream at the same time, or <=-<=1 if they will never scream at the same time. | [
"20 2\n9 19\n",
"2 1\n16 12\n"
] | [
"82\n",
"-1\n"
] | In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82.
In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time. | [
{
"input": "20 2\n9 19",
"output": "82"
},
{
"input": "2 1\n16 12",
"output": "-1"
},
{
"input": "39 52\n88 78",
"output": "1222"
},
{
"input": "59 96\n34 48",
"output": "1748"
},
{
"input": "87 37\n91 29",
"output": "211"
},
{
"input": "11 81\n49 7",
"output": "301"
},
{
"input": "39 21\n95 89",
"output": "3414"
},
{
"input": "59 70\n48 54",
"output": "1014"
},
{
"input": "87 22\n98 32",
"output": "718"
},
{
"input": "15 63\n51 13",
"output": "-1"
},
{
"input": "39 7\n97 91",
"output": "1255"
},
{
"input": "18 18\n71 71",
"output": "1278"
},
{
"input": "46 71\n16 49",
"output": "209"
},
{
"input": "70 11\n74 27",
"output": "2321"
},
{
"input": "94 55\n20 96",
"output": "-1"
},
{
"input": "18 4\n77 78",
"output": "1156"
},
{
"input": "46 44\n23 55",
"output": "-1"
},
{
"input": "74 88\n77 37",
"output": "1346"
},
{
"input": "94 37\n34 7",
"output": "789"
},
{
"input": "22 81\n80 88",
"output": "-1"
},
{
"input": "46 30\n34 62",
"output": "674"
},
{
"input": "40 4\n81 40",
"output": "364"
},
{
"input": "69 48\n39 9",
"output": "48"
},
{
"input": "89 93\n84 87",
"output": "5967"
},
{
"input": "17 45\n42 65",
"output": "317"
},
{
"input": "41 85\n95 46",
"output": "331"
},
{
"input": "69 30\n41 16",
"output": "1410"
},
{
"input": "93 74\n99 93",
"output": "-1"
},
{
"input": "17 19\n44 75",
"output": "427"
},
{
"input": "45 63\n98 53",
"output": "3483"
},
{
"input": "69 11\n48 34",
"output": "-1"
},
{
"input": "55 94\n3 96",
"output": "204"
},
{
"input": "100 100\n100 100",
"output": "100"
},
{
"input": "1 1\n1 1",
"output": "1"
},
{
"input": "1 1\n1 100",
"output": "100"
},
{
"input": "1 100\n100 1",
"output": "101"
},
{
"input": "98 1\n99 100",
"output": "9703"
},
{
"input": "98 1\n99 2",
"output": "9605"
},
{
"input": "97 2\n99 100",
"output": "4852"
},
{
"input": "3 3\n3 1",
"output": "-1"
},
{
"input": "3 2\n7 2",
"output": "2"
},
{
"input": "2 3\n2 5",
"output": "5"
},
{
"input": "2 3\n2 3",
"output": "3"
},
{
"input": "100 3\n100 5",
"output": "-1"
},
{
"input": "6 10\n12 14",
"output": "-1"
},
{
"input": "4 2\n4 4",
"output": "-1"
},
{
"input": "2 3\n2 2",
"output": "-1"
},
{
"input": "2 3\n4 99",
"output": "99"
},
{
"input": "1 5\n1 5",
"output": "5"
},
{
"input": "1 100\n3 1",
"output": "100"
},
{
"input": "2 2\n2 1",
"output": "-1"
},
{
"input": "2 10\n6 20",
"output": "20"
},
{
"input": "2 2\n2 10",
"output": "10"
},
{
"input": "3 7\n3 6",
"output": "-1"
},
{
"input": "1 100\n1 100",
"output": "100"
},
{
"input": "7 25\n39 85",
"output": "319"
},
{
"input": "84 82\n38 6",
"output": "82"
},
{
"input": "7 7\n7 14",
"output": "14"
}
] | 155 | 21,401,600 | 3 | 1,725 |
|
47 | Triangular numbers | [
"brute force",
"math"
] | A. Triangular numbers | 2 | 256 | A triangular number is the number of dots in an equilateral triangle uniformly filled with dots. For example, three dots can be arranged in a triangle; thus three is a triangular number. The *n*-th triangular number is the number of dots in a triangle with *n* dots on a side. . You can learn more about these numbers from Wikipedia (http://en.wikipedia.org/wiki/Triangular_number).
Your task is to find out if a given integer is a triangular number. | The first line contains the single number *n* (1<=β€<=*n*<=β€<=500) β the given integer. | If the given integer is a triangular number output YES, otherwise output NO. | [
"1\n",
"2\n",
"3\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "1",
"output": "YES"
},
{
"input": "2",
"output": "NO"
},
{
"input": "3",
"output": "YES"
},
{
"input": "4",
"output": "NO"
},
{
"input": "5",
"output": "NO"
},
{
"input": "6",
"output": "YES"
},
{
"input": "7",
"output": "NO"
},
{
"input": "8",
"output": "NO"
},
{
"input": "12",
"output": "NO"
},
{
"input": "10",
"output": "YES"
},
{
"input": "11",
"output": "NO"
},
{
"input": "9",
"output": "NO"
},
{
"input": "14",
"output": "NO"
},
{
"input": "15",
"output": "YES"
},
{
"input": "16",
"output": "NO"
},
{
"input": "20",
"output": "NO"
},
{
"input": "21",
"output": "YES"
},
{
"input": "22",
"output": "NO"
},
{
"input": "121",
"output": "NO"
},
{
"input": "135",
"output": "NO"
},
{
"input": "136",
"output": "YES"
},
{
"input": "137",
"output": "NO"
},
{
"input": "152",
"output": "NO"
},
{
"input": "153",
"output": "YES"
},
{
"input": "154",
"output": "NO"
},
{
"input": "171",
"output": "YES"
},
{
"input": "189",
"output": "NO"
},
{
"input": "190",
"output": "YES"
},
{
"input": "191",
"output": "NO"
},
{
"input": "210",
"output": "YES"
},
{
"input": "211",
"output": "NO"
},
{
"input": "231",
"output": "YES"
},
{
"input": "232",
"output": "NO"
},
{
"input": "252",
"output": "NO"
},
{
"input": "253",
"output": "YES"
},
{
"input": "254",
"output": "NO"
},
{
"input": "275",
"output": "NO"
},
{
"input": "276",
"output": "YES"
},
{
"input": "277",
"output": "NO"
},
{
"input": "299",
"output": "NO"
},
{
"input": "300",
"output": "YES"
},
{
"input": "301",
"output": "NO"
},
{
"input": "324",
"output": "NO"
},
{
"input": "325",
"output": "YES"
},
{
"input": "326",
"output": "NO"
},
{
"input": "350",
"output": "NO"
},
{
"input": "351",
"output": "YES"
},
{
"input": "352",
"output": "NO"
},
{
"input": "377",
"output": "NO"
},
{
"input": "378",
"output": "YES"
},
{
"input": "406",
"output": "YES"
},
{
"input": "434",
"output": "NO"
},
{
"input": "435",
"output": "YES"
},
{
"input": "464",
"output": "NO"
},
{
"input": "465",
"output": "YES"
},
{
"input": "495",
"output": "NO"
},
{
"input": "496",
"output": "YES"
},
{
"input": "497",
"output": "NO"
},
{
"input": "331",
"output": "NO"
},
{
"input": "413",
"output": "NO"
},
{
"input": "177",
"output": "NO"
},
{
"input": "122",
"output": "NO"
},
{
"input": "478",
"output": "NO"
},
{
"input": "188",
"output": "NO"
},
{
"input": "21",
"output": "YES"
},
{
"input": "474",
"output": "NO"
},
{
"input": "21",
"output": "YES"
},
{
"input": "48",
"output": "NO"
},
{
"input": "498",
"output": "NO"
},
{
"input": "499",
"output": "NO"
},
{
"input": "500",
"output": "NO"
}
] | 248 | 0 | 3.938 | 1,726 |
1,009 | Intercity Travelling | [
"combinatorics",
"math",
"probabilities"
] | null | null | Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.
The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is $n$ km. Let's say that Moscow is situated at the point with coordinate $0$ km, and Saratov β at coordinate $n$ km.
Driving for a long time may be really difficult. Formally, if Leha has already covered $i$ kilometers since he stopped to have a rest, he considers the difficulty of covering $(i + 1)$-th kilometer as $a_{i + 1}$. It is guaranteed that for every $i \in [1, n - 1]$ $a_i \le a_{i + 1}$. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.
Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from $1$ to $n - 1$ may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty $a_1$, the kilometer after it β difficulty $a_2$, and so on.
For example, if $n = 5$ and there is a rest site in coordinate $2$, the difficulty of journey will be $2a_1 + 2a_2 + a_3$: the first kilometer will have difficulty $a_1$, the second one β $a_2$, then Leha will have a rest, and the third kilometer will have difficulty $a_1$, the fourth β $a_2$, and the last one β $a_3$. Another example: if $n = 7$ and there are rest sites in coordinates $1$ and $5$, the difficulty of Leha's journey is $3a_1 + 2a_2 + a_3 + a_4$.
Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are $2^{n - 1}$ different distributions of rest sites (two distributions are different if there exists some point $x$ such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate $p$ β the expected value of difficulty of his journey.
Obviously, $p \cdot 2^{n - 1}$ is an integer number. You have to calculate it modulo $998244353$. | The first line contains one number $n$ ($1 \le n \le 10^6$) β the distance from Moscow to Saratov.
The second line contains $n$ integer numbers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_1 \le a_2 \le \dots \le a_n \le 10^6$), where $a_i$ is the difficulty of $i$-th kilometer after Leha has rested. | Print one number β $p \cdot 2^{n - 1}$, taken modulo $998244353$. | [
"2\n1 2\n",
"4\n1 3 3 7\n"
] | [
"5\n",
"60\n"
] | none | [
{
"input": "2\n1 2",
"output": "5"
},
{
"input": "4\n1 3 3 7",
"output": "60"
},
{
"input": "100\n3 3 3 4 7 8 8 8 9 9 10 12 12 13 14 14 15 15 16 17 17 20 21 21 22 22 23 25 29 31 36 37 37 38 39 40 41 41 41 42 43 44 45 46 46 47 47 49 49 49 51 52 52 53 54 55 59 59 59 60 62 63 63 64 66 69 70 71 71 72 74 76 76 77 77 78 78 79 80 81 81 82 82 84 85 86 87 87 87 89 91 92 92 92 92 97 98 99 100 100",
"output": "758086002"
},
{
"input": "1\n12",
"output": "12"
}
] | 1,404 | 61,440,000 | -1 | 1,727 |
|
276 | Little Girl and Game | [
"games",
"greedy"
] | null | null | The Little Girl loves problems on games very much. Here's one of them.
Two players have got a string *s*, consisting of lowercase English letters. They play a game that is described by the following rules:
- The players move in turns; In one move the player can remove an arbitrary letter from string *s*. - If the player before his turn can reorder the letters in string *s* so as to get a palindrome, this player wins. A palindrome is a string that reads the same both ways (from left to right, and vice versa). For example, string "abba" is a palindrome and string "abc" isn't.
Determine which player will win, provided that both sides play optimally well β the one who moves first or the one who moves second. | The input contains a single line, containing string *s* (1<=β€<=|*s*|<=<=β€<=<=103). String *s* consists of lowercase English letters. | In a single line print word "First" if the first player wins (provided that both players play optimally well). Otherwise, print word "Second". Print the words without the quotes. | [
"aba\n",
"abca\n"
] | [
"First\n",
"Second\n"
] | none | [
{
"input": "aba",
"output": "First"
},
{
"input": "abca",
"output": "Second"
},
{
"input": "aabb",
"output": "First"
},
{
"input": "ctjxzuimsxnarlciuynqeoqmmbqtagszuo",
"output": "Second"
},
{
"input": "gevqgtaorjixsxnbcoybr",
"output": "First"
},
{
"input": "xvhtcbtouuddhylxhplgjxwlo",
"output": "First"
},
{
"input": "knaxhkbokmtfvnjvlsbrfoefpjpkqwlumeqqbeohodnwevhllkylposdpjuoizyunuxivzrjofiyxxiliuwhkjqpkqxukxroivfhikxjdtwcqngqswptdwrywxszxrqojjphzwzxqftnfhkapeejdgckfyrxtpuipfljsjwgpjfatmxpylpnerllshuvkbomlpghjrxcgxvktgeyuhrcwgvdmppqnkdmjtxukzlzqhfbgrishuhkyggkpstvqabpxoqjuovwjwcmazmvpfpnljdgpokpatjnvwacotkvxheorzbsrazldsquijzkmtmqahakjrjvzkquvayxpqrmqqcknilpqpjapagezonfpz",
"output": "Second"
},
{
"input": "desktciwoidfuswycratvovutcgjrcyzmilsmadzaegseetexygedzxdmorxzxgiqhcuppshcsjcozkopebegfmxzxxagzwoymlghgjexcgfojychyt",
"output": "First"
},
{
"input": "gfhuidxgxpxduqrfnqrnefgtyxgmrtehmddjkddwdiayyilaknxhlxszeslnsjpcrwnoqubmbpcehiftteirkfvbtfyibiikdaxmondnawtvqccctdxrjcfxqwqhvvrqmhqflbzskrayvruqvqijrmikucwzodxvufwxpxxjxlifdjzxrttjzatafkbzsjupsiefmipdufqltedjlytphzppoevxawjdhbxgennevbvdgpoeihasycctyddenzypoprchkoioouhcexjqwjflxvkgpgjatstlmledxasecfhwvabzwviywsiaryqrxyeceefblherqjevdzkfxslqiytwzz",
"output": "First"
},
{
"input": "fezzkpyctjvvqtncmmjsitrxaliyhirspnjjngvzdoudrkkvvdiwcwtcxobpobzukegtcrwsgxxzlcphdxkbxdximqbycaicfdeqlvzboptfimkzvjzdsvahorqqhcirpkhtwjkplitpacpkpbhnxtoxuoqsxcxnhtrmzvexmpvlethbkvmlzftimjnidrzvcunbpysvukzgwghjmwrvstsunaocnoqohcsggtrwxiworkliqejajewbrtdwgnyynpupbrrvtfqtlaaq",
"output": "Second"
},
{
"input": "tsvxmeixijyavdalmrvscwohzubhhgsocdvnjmjtctojbxxpezzbgfltixwgzmkfwdnlhidhrdgyajggmrvmwaoydodjmzqvgabyszfqcuhwdncyfqvmackvijgpjyiauxljvvwgiofdxccwmybdfcfcrqppbvbagmnvvvhngxauwbpourviyfokwjweypzzrrzjcmddnpoaqgqfgglssjnlshrerfffmrwhapzknxveiqixflykjbnpivogtdpyjakwrdoklsbvbkjhdojfnuwbpcfdycwxecysbyjfvoykxsxgg",
"output": "First"
},
{
"input": "upgqmhfmfnodsyosgqswugfvpdxhtkxvhlsxrjiqlojchoddxkpsamwmuvopdbncymcgrkurwlxerexgswricuqxhvqvgekeofkgqabypamozmyjyfvpifsaotnyzqydcenphcsmplekinwkmwzpjnlapfdbhxjdcnarlgkfgxzfbpgsuxqfyhnxjhtojrlnprnxprfbkkcyriqztjeeepkzgzcaiutvbqqofyhddfebozhvtvrigtidxqmydjxegxipakzjcnenjkdroyjmxugj",
"output": "Second"
},
{
"input": "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccccddddddddddeeeeeeeeeeffffgggghhhhiiiijjjjqqqqwwwweeeerrrrttttyyyyuuuuiiiiooooppppaaaassssddddffffgggghhhhjjjjkkkkllllzzzzxxxxccccvvvvbbbbnnnnmmmm",
"output": "First"
},
{
"input": "vnvtvnxjrtffdhrfvczzoyeokjabxcilmmsrhwuakghvuabcmfpmblyroodmhfivmhqoiqhapoglwaluewhqkunzitmvijaictjdncivccedfpaezcnpwemlohbhjjlqsonuclaumgbzjamsrhuzqdqtitygggsnruuccdtxkgbdd",
"output": "First"
},
{
"input": "vqdtkbvlbdyndheoiiwqhnvcmmhnhsmwwrvesnpdfxvprqbwzbodoihrywagphlsrcbtnvppjsquuuzkjazaenienjiyctyajsqdfsdiedzugkymgzllvpxfetkwfabbiotjcknzdwsvmbbuqrxrulvgljagvxdmfsqtcczhifhoghqgffkbviphbabwiaqburerfkbqfjbptkwlahysrrfwjbqfnrgnsnsukqqcxxwqtuhvdzqmpfwrbqzdwxcaifuyhvojgurmchh",
"output": "First"
},
{
"input": "hxueikegwnrctlciwguepdsgupguykrntbszeqzzbpdlouwnmqgzcxejidstxyxhdlnttnibxstduwiflouzfswfikdudkazoefawm",
"output": "Second"
},
{
"input": "ershkhsywqftixappwqzoojtnamvqjbyfauvuubwpctspioqusnnivwsiyszfhlrskbswaiaczurygcioonjcndntwvrlaejyrghfnecltqytfmkvjxuujifgtujrqsisdawpwgttxynewiqhdhronamabysvpxankxeybcjqttbqnciwuqiehzyfjoedaradqnfthuuwrezwrkjiytpgwfwbslawbiezdbdltenjlaygwaxddplgseiaojndqjcopvolqbvnacuvfvirzbrnlnyjixngeevcggmirzatenjihpgnyfjhgsjgzepohbyhmzbatfwuorwutavlqsogrvcjpqziuifrhurq",
"output": "First"
},
{
"input": "qilwpsuxogazrfgfznngwklnioueuccyjfatjoizcctgsweitzofwkyjustizbopzwtaqxbtovkdrxeplukrcuozhpymldstbbfynkgsmafigetvzkxloxqtphvtwkgfjkiczttcsxkjpsoutdpzxytrsqgjtbdljjrbmkudrkodfvcwkcuggbsthxdyogeeyfuyhmnwgyuatfkvchavpzadfacckdurlbqjkthqbnirzzbpusxcenkpgtizayjmsahvobobudfeaewcqmrlxxnocqzmkessnguxkiccrxyvnxxlqnqfwuzmupk",
"output": "First"
},
{
"input": "opfokvwzpllctflkphutcrkferbjyyrasqqkrcvoymyrxwaudgsugcqveccymdplxmtlzfoptmrapfeizpnnhbzlkuyznwacnswibxhqunazbhdvrlidghisuqunstbuevjzimvlfvopgqxrvahhngnaumgywscfrfwfpnfxwhfrelbunmedvkssykwjyartxjiplerntzkpiiaalijiwhyuhxlvhxpkgfypvrpqqsacuwocdampnnhvibsbolyduvscsjfayxpldyvqzjbqojjxdvxtctwtifcdfcaoonzbgegyllngwvjivtxeezoabhsihcuvgvdsgjtzbzwovjshvwrljkxavowovzrfpdufpogdtujkerdorpboufrxhyswirjehgsxthlvjufvahdpeajidqipwaxokbeycrievgcfkyuyavaq",
"output": "Second"
},
{
"input": "a",
"output": "First"
},
{
"input": "ab",
"output": "Second"
},
{
"input": "abacaba",
"output": "First"
},
{
"input": "abazaba",
"output": "First"
},
{
"input": "zz",
"output": "First"
},
{
"input": "aassddxyz",
"output": "First"
},
{
"input": "abcdefghijklmnopqrstuvwxyz",
"output": "Second"
},
{
"input": "aabc",
"output": "Second"
},
{
"input": "abcabc",
"output": "First"
},
{
"input": "aaabbbccdd",
"output": "Second"
},
{
"input": "aabbcccc",
"output": "First"
}
] | 62 | 0 | 0 | 1,730 |
|
400 | Inna and New Matrix of Candies | [
"brute force",
"implementation",
"schedules"
] | null | null | Inna likes sweets and a game called the "Candy Matrix". Today, she came up with the new game "Candy Matrix 2: Reload".
The field for the new game is a rectangle table of size *n*<=Γ<=*m*. Each line of the table contains one cell with a dwarf figurine, one cell with a candy, the other cells of the line are empty. The game lasts for several moves. During each move the player should choose all lines of the matrix where dwarf is not on the cell with candy and shout "Let's go!". After that, all the dwarves from the chosen lines start to simultaneously move to the right. During each second, each dwarf goes to the adjacent cell that is located to the right of its current cell. The movement continues until one of the following events occurs:
- some dwarf in one of the chosen lines is located in the rightmost cell of his row; - some dwarf in the chosen lines is located in the cell with the candy.
The point of the game is to transport all the dwarves to the candy cells.
Inna is fabulous, as she came up with such an interesting game. But what about you? Your task is to play this game optimally well. Specifically, you should say by the given game field what minimum number of moves the player needs to reach the goal of the game. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*<=β€<=1000;Β 2<=β€<=*m*<=β€<=1000).
Next *n* lines each contain *m* characters β the game field for the "Candy Martix 2: Reload". Character "*" represents an empty cell of the field, character "G" represents a dwarf and character "S" represents a candy. The matrix doesn't contain other characters. It is guaranteed that each line contains exactly one character "G" and one character "S". | In a single line print a single integer β either the minimum number of moves needed to achieve the aim of the game, or -1, if the aim cannot be achieved on the given game field. | [
"3 4\n*G*S\nG**S\n*G*S\n",
"1 3\nS*G\n"
] | [
"2\n",
"-1\n"
] | none | [
{
"input": "3 4\n*G*S\nG**S\n*G*S",
"output": "2"
},
{
"input": "1 3\nS*G",
"output": "-1"
},
{
"input": "10 10\nG********S\n*G*******S\n**G******S\n***G*****S\n****G****S\n*****G***S\n******G**S\n*******G*S\n********GS\nG********S",
"output": "9"
},
{
"input": "5 10\nG***S*****\nG****S****\n***GS*****\nG*S*******\nG***S*****",
"output": "4"
},
{
"input": "4 8\nG*S*****\n****G*S*\nG*****S*\n**G***S*",
"output": "3"
},
{
"input": "4 10\n***G****S*\n*****GS***\nG****S****\nG*******S*",
"output": "3"
},
{
"input": "1 2\nSG",
"output": "-1"
},
{
"input": "1 2\nGS",
"output": "1"
},
{
"input": "1 4\nSG**",
"output": "-1"
}
] | 62 | 0 | 0 | 1,736 |
|
508 | Anton and currency you all know | [
"greedy",
"math",
"strings"
] | null | null | Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is now assumed to be an integer.
Reliable sources have informed the financier Anton of some information about the exchange rate of currency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that today the exchange rate is an odd positive integer *n*. Help Anton to determine the exchange rate of currency you all know for tomorrow! | The first line contains an odd positive integer *n*Β β the exchange rate of currency you all know for today. The length of number *n*'s representation is within range from 2 to 105, inclusive. The representation of *n* doesn't contain any leading zeroes. | If the information about tomorrow's exchange rate is inconsistent, that is, there is no integer that meets the condition, print <=-<=1.
Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today's exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes. | [
"527\n",
"4573\n",
"1357997531\n"
] | [
"572\n",
"3574\n",
"-1\n"
] | none | [
{
"input": "527",
"output": "572"
},
{
"input": "4573",
"output": "3574"
},
{
"input": "1357997531",
"output": "-1"
},
{
"input": "444443",
"output": "444434"
},
{
"input": "22227",
"output": "72222"
},
{
"input": "24683",
"output": "34682"
},
{
"input": "11",
"output": "-1"
},
{
"input": "1435678543",
"output": "1435678534"
},
{
"input": "4250769",
"output": "9250764"
},
{
"input": "4052769",
"output": "9052764"
},
{
"input": "5685341",
"output": "5685314"
},
{
"input": "1111111111111111231",
"output": "1111111111111111132"
},
{
"input": "333333332379",
"output": "333333339372"
},
{
"input": "85",
"output": "58"
},
{
"input": "7700016673",
"output": "7730016670"
},
{
"input": "35451519805848712272404365322858764249299938505103",
"output": "35451519835848712272404365322858764249299938505100"
},
{
"input": "4314752277691991627730686134692292422155753465948025897701703862445837045929984759093775762579123919",
"output": "9314752277691991627730686134692292422155753465948025897701703862445837045929984759093775762579123914"
},
{
"input": "21",
"output": "12"
},
{
"input": "101",
"output": "110"
},
{
"input": "503147",
"output": "573140"
},
{
"input": "333333333333333333333",
"output": "-1"
},
{
"input": "55555555555555555555555",
"output": "-1"
},
{
"input": "99",
"output": "-1"
},
{
"input": "23759",
"output": "93752"
},
{
"input": "235749",
"output": "935742"
},
{
"input": "435729",
"output": "935724"
},
{
"input": "8623",
"output": "8632"
},
{
"input": "109",
"output": "190"
},
{
"input": "20000000000000000000001",
"output": "21000000000000000000000"
},
{
"input": "1001",
"output": "1100"
}
] | 62 | 0 | 0 | 1,740 |
|
799 | T-shirt buying | [
"data structures",
"implementation"
] | null | null | A new pack of *n* t-shirts came to a shop. Each of the t-shirts is characterized by three integers *p**i*, *a**i* and *b**i*, where *p**i* is the price of the *i*-th t-shirt, *a**i* is front color of the *i*-th t-shirt and *b**i* is back color of the *i*-th t-shirt. All values *p**i* are distinct, and values *a**i* and *b**i* are integers from 1 to 3.
*m* buyers will come to the shop. Each of them wants to buy exactly one t-shirt. For the *j*-th buyer we know his favorite color *c**j*.
A buyer agrees to buy a t-shirt, if at least one side (front or back) is painted in his favorite color. Among all t-shirts that have colors acceptable to this buyer he will choose the cheapest one. If there are no such t-shirts, the buyer won't buy anything. Assume that the buyers come one by one, and each buyer is served only after the previous one is served.
You are to compute the prices each buyer will pay for t-shirts. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of t-shirts.
The following line contains sequence of integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=1<=000<=000<=000), where *p**i* equals to the price of the *i*-th t-shirt.
The following line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=3), where *a**i* equals to the front color of the *i*-th t-shirt.
The following line contains sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=3), where *b**i* equals to the back color of the *i*-th t-shirt.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=200<=000)Β β the number of buyers.
The following line contains sequence *c*1,<=*c*2,<=...,<=*c**m* (1<=β€<=*c**j*<=β€<=3), where *c**j* equals to the favorite color of the *j*-th buyer. The buyers will come to the shop in the order they are given in the input. Each buyer is served only after the previous one is served. | Print to the first line *m* integers β the *j*-th integer should be equal to the price of the t-shirt which the *j*-th buyer will buy. If the *j*-th buyer won't buy anything, print -1. | [
"5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1\n",
"2\n1000000000 1\n1 1\n1 2\n2\n2 1\n"
] | [
"200 400 300 500 911 -1 \n",
"1 1000000000 \n"
] | none | [
{
"input": "5\n300 200 400 500 911\n1 2 1 2 3\n2 1 3 2 1\n6\n2 3 1 2 1 1",
"output": "200 400 300 500 911 -1 "
},
{
"input": "2\n1000000000 1\n1 1\n1 2\n2\n2 1",
"output": "1 1000000000 "
},
{
"input": "10\n251034796 163562337 995167403 531046374 341924810 828969071 971837553 183763940 857690534 687685084\n3 2 1 3 2 3 1 3 2 1\n2 3 3 1 2 3 2 3 3 2\n10\n1 3 2 3 2 3 3 1 2 3",
"output": "531046374 163562337 251034796 183763940 341924810 828969071 857690534 687685084 971837553 995167403 "
},
{
"input": "20\n414468312 20329584 106106409 584924603 666547477 670032002 726095027 276840253 368277336 940941705 531635095 213813062 440421387 959075599 240727854 495316522 838268432 786936631 586382273 806443734\n3 1 2 3 3 2 2 1 3 2 3 2 3 3 3 2 1 3 1 2\n3 1 2 2 2 2 3 1 2 3 2 1 1 2 3 1 2 3 3 2\n40\n1 1 2 1 3 2 3 1 3 3 1 2 3 1 1 1 2 3 3 1 3 1 3 1 2 2 3 3 1 2 1 2 3 2 2 1 2 1 2 2",
"output": "20329584 213813062 106106409 276840253 240727854 368277336 414468312 440421387 531635095 584924603 495316522 666547477 586382273 838268432 -1 -1 670032002 726095027 786936631 -1 940941705 -1 959075599 -1 806443734 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "
},
{
"input": "1\n529469903\n1\n3\n1\n3",
"output": "529469903 "
}
] | 3,000 | 17,612,800 | 0 | 1,741 |
|
156 | Clues | [
"combinatorics",
"graphs"
] | null | null | As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues *A* and *B* and the direct link between clues *B* and *A* is the same thing. No more than one direct link can exist between two clues.
Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues *A* and *B* are considered linked either if there is a direct link between them or if there is a direct link between *A* and some other clue *C* which is linked to *B*.
Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals *T*.
Please count the number of different ways to find exactly *T* direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way.
As the number of different ways can turn out rather big, print it modulo *k*. | The first line contains three space-separated integers *n*,<=*m*,<=*k* (1<=β€<=*n*<=β€<=105,<=0<=β€<=*m*<=β€<=105, 1<=β€<=*k*<=β€<=109) β the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation.
Each of next *m* lines contains two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*,<=*a*<=β <=*b*), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual. | Print the single number β the answer to the problem modulo *k*. | [
"2 0 1000000000\n",
"3 0 100\n",
"4 1 1000000000\n1 4\n"
] | [
"1\n",
"3\n",
"8\n"
] | The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link.
The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime β there are 3 ways to do it.
The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime. | [
{
"input": "2 0 1000000000",
"output": "1"
},
{
"input": "3 0 100",
"output": "3"
},
{
"input": "4 1 1000000000\n1 4",
"output": "8"
},
{
"input": "6 4 100000\n1 4\n4 6\n6 1\n2 5",
"output": "36"
},
{
"input": "10 0 123456789",
"output": "100000000"
},
{
"input": "10 5 1000000000\n1 2\n4 3\n5 6\n8 7\n10 9",
"output": "32000"
},
{
"input": "8 4 17\n1 2\n2 3\n3 4\n4 1",
"output": "8"
},
{
"input": "9 6 342597160\n1 2\n3 4\n4 5\n6 7\n7 8\n8 9",
"output": "216"
},
{
"input": "1 0 1",
"output": "0"
},
{
"input": "15 10 1\n1 2\n4 5\n6 3\n11 8\n8 5\n5 9\n9 1\n11 12\n12 1\n2 8",
"output": "0"
},
{
"input": "8 8 999999937\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n8 7",
"output": "1"
},
{
"input": "100000 0 1000000000",
"output": "0"
},
{
"input": "100000 0 1",
"output": "0"
},
{
"input": "9 11 498920381\n2 8\n5 4\n1 8\n8 3\n4 9\n3 6\n8 9\n1 7\n5 1\n5 6\n9 6",
"output": "1"
},
{
"input": "2 0 753780649",
"output": "1"
},
{
"input": "1 0 997185958",
"output": "1"
},
{
"input": "10 36 279447540\n10 7\n10 8\n1 8\n1 5\n4 5\n9 5\n3 9\n7 3\n10 4\n8 9\n2 10\n6 2\n4 8\n10 3\n1 4\n10 1\n10 6\n8 3\n3 6\n9 7\n10 5\n6 9\n3 1\n8 6\n4 9\n5 3\n9 10\n7 2\n2 4\n7 4\n5 6\n5 8\n7 5\n5 2\n6 7\n1 9",
"output": "1"
},
{
"input": "7 7 302838679\n5 3\n4 1\n5 4\n6 5\n1 6\n3 2\n6 4",
"output": "6"
},
{
"input": "6 1 310732484\n4 2",
"output": "432"
},
{
"input": "10 7 587143295\n1 10\n7 1\n1 8\n7 10\n8 10\n4 8\n6 8",
"output": "6000"
},
{
"input": "3 3 975373207\n1 2\n1 3\n3 2",
"output": "1"
},
{
"input": "9 33 321578376\n9 5\n6 3\n8 4\n4 1\n3 5\n2 6\n8 2\n7 6\n7 9\n8 6\n4 5\n1 6\n1 2\n5 6\n9 4\n7 8\n3 9\n9 6\n4 7\n7 2\n1 8\n4 6\n8 3\n3 7\n8 9\n5 7\n3 4\n7 1\n9 2\n5 1\n2 5\n9 1\n3 2",
"output": "1"
},
{
"input": "5 10 93196990\n1 5\n1 4\n4 2\n1 3\n3 4\n1 2\n5 2\n5 4\n5 3\n2 3",
"output": "1"
},
{
"input": "1 0 773734495",
"output": "1"
},
{
"input": "2 1 719418546\n1 2",
"output": "1"
},
{
"input": "3 2 21502109\n3 2\n1 2",
"output": "1"
},
{
"input": "9 35 480175322\n6 3\n8 6\n7 5\n7 9\n3 4\n2 8\n5 3\n4 5\n4 6\n7 1\n7 6\n2 5\n8 3\n6 9\n8 4\n8 5\n6 1\n8 1\n3 2\n5 1\n8 9\n3 1\n8 7\n5 6\n5 9\n4 9\n7 4\n2 7\n3 9\n2 4\n7 3\n9 1\n2 9\n1 4\n1 2",
"output": "1"
},
{
"input": "5 7 729985252\n2 3\n3 1\n2 5\n1 5\n1 2\n1 4\n4 3",
"output": "1"
},
{
"input": "2 1 819865995\n2 1",
"output": "1"
},
{
"input": "10 0 766953983",
"output": "100000000"
},
{
"input": "2 1 855341703\n2 1",
"output": "1"
},
{
"input": "10 30 407595309\n3 6\n6 10\n6 7\n7 10\n7 8\n3 10\n3 4\n1 4\n9 10\n8 4\n3 7\n5 1\n2 4\n6 2\n8 9\n10 5\n7 5\n10 4\n5 8\n8 2\n10 2\n1 6\n4 7\n2 3\n5 6\n8 10\n3 5\n1 8\n9 7\n1 9",
"output": "1"
},
{
"input": "1 0 21080115",
"output": "1"
},
{
"input": "57 28 776442742\n31 10\n25 28\n51 45\n14 40\n21 52\n53 51\n52 53\n4 6\n51 35\n53 15\n17 16\n40 44\n37 51\n33 43\n55 40\n42 16\n30 8\n19 45\n7 27\n31 8\n49 8\n43 44\n45 3\n16 22\n32 36\n52 36\n5 26\n2 23",
"output": "135540294"
},
{
"input": "30 70 288262020\n27 18\n5 19\n23 17\n16 17\n29 17\n1 22\n23 5\n10 13\n22 26\n14 3\n8 3\n29 9\n9 1\n3 9\n16 4\n9 22\n10 22\n20 1\n3 7\n23 19\n26 8\n24 1\n5 7\n28 29\n20 11\n16 12\n6 9\n24 29\n30 4\n5 26\n18 21\n5 21\n30 6\n12 13\n16 23\n28 14\n30 1\n7 27\n7 19\n27 17\n5 30\n30 27\n28 30\n12 28\n27 9\n30 26\n20 18\n21 16\n8 30\n4 26\n13 22\n2 14\n12 30\n4 2\n6 12\n29 25\n19 29\n14 15\n3 23\n10 28\n7 1\n21 10\n4 12\n1 14\n7 21\n21 8\n17 26\n7 6\n26 29\n9 8",
"output": "1"
},
{
"input": "99 12 832839308\n66 23\n36 5\n16 57\n70 62\n94 96\n63 33\n99 23\n63 10\n6 85\n73 23\n69 46\n72 95",
"output": "71450536"
},
{
"input": "30 18 918975816\n30 18\n23 1\n21 14\n14 8\n18 9\n23 29\n3 23\n29 19\n18 4\n27 19\n30 2\n9 10\n9 28\n16 15\n10 6\n18 12\n23 9\n19 14",
"output": "782410104"
},
{
"input": "83 33 367711297\n14 74\n26 22\n55 19\n8 70\n6 42\n53 49\n54 56\n52 17\n62 44\n78 61\n76 4\n78 30\n51 2\n31 42\n33 67\n45 41\n64 62\n15 25\n33 35\n37 20\n38 65\n65 83\n61 14\n20 67\n62 47\n7 34\n78 41\n38 83\n26 69\n54 58\n11 62\n30 55\n15 74",
"output": "131377693"
},
{
"input": "24 68 862907549\n6 9\n16 22\n11 23\n12 17\n18 2\n15 5\n5 22\n16 4\n21 9\n7 11\n19 16\n9 13\n21 20\n5 24\n7 12\n17 1\n24 21\n23 7\n16 17\n16 18\n10 13\n18 7\n8 21\n13 5\n10 18\n4 11\n21 6\n15 13\n2 1\n20 16\n11 16\n22 19\n2 4\n21 1\n6 18\n24 12\n21 19\n6 14\n22 24\n11 20\n2 19\n1 11\n24 18\n14 8\n10 24\n5 3\n11 3\n17 4\n4 20\n2 10\n12 11\n24 7\n23 16\n2 3\n19 24\n22 1\n22 4\n4 6\n3 4\n11 13\n6 5\n18 23\n4 23\n22 13\n20 5\n2 5\n2 11\n9 5",
"output": "1"
},
{
"input": "86 23 608266393\n62 78\n44 84\n42 37\n20 24\n40 36\n41 76\n14 38\n80 72\n39 52\n31 58\n71 17\n81 6\n32 65\n11 69\n43 86\n85 59\n28 77\n78 64\n15 19\n36 39\n53 49\n48 75\n33 85",
"output": "235915236"
},
{
"input": "47 51 283106191\n18 14\n30 26\n24 2\n18 41\n35 31\n16 24\n29 39\n6 12\n17 21\n7 19\n36 16\n27 39\n28 34\n22 35\n28 43\n40 5\n2 26\n18 16\n27 13\n21 6\n19 5\n35 30\n13 31\n7 10\n25 7\n44 42\n45 1\n35 47\n11 28\n47 46\n18 15\n27 16\n24 41\n10 8\n25 41\n4 40\n5 11\n24 6\n10 17\n41 38\n47 28\n8 29\n25 24\n35 37\n44 17\n24 47\n8 32\n33 11\n26 28\n23 9\n5 9",
"output": "189974"
},
{
"input": "67 2 818380264\n4 52\n15 44",
"output": "517849052"
},
{
"input": "10 45 220178113\n9 1\n8 1\n5 8\n1 5\n7 8\n6 7\n7 9\n6 2\n3 2\n1 4\n8 3\n8 9\n3 6\n4 5\n5 3\n10 4\n3 9\n9 6\n5 9\n2 9\n10 7\n1 10\n9 4\n3 10\n2 5\n7 1\n6 10\n6 5\n8 6\n8 4\n8 10\n1 6\n4 2\n9 10\n2 10\n7 3\n6 4\n7 5\n1 2\n4 3\n10 5\n4 7\n3 1\n7 2\n8 2",
"output": "1"
},
{
"input": "588 32 634894588\n535 26\n562 406\n70 368\n357 513\n108 361\n515 5\n159 56\n522 81\n169 229\n312 252\n492 43\n476 405\n524 555\n537 169\n142 149\n586 112\n7 159\n76 370\n295 376\n33 455\n278 225\n377 88\n526 308\n517 303\n300 576\n230 493\n588 525\n177 312\n356 215\n515 34\n196 236\n323 9",
"output": "478655040"
},
{
"input": "3 2 11\n1 2\n2 3",
"output": "1"
},
{
"input": "2 1 1000\n1 2",
"output": "1"
},
{
"input": "1 0 10000",
"output": "1"
},
{
"input": "5 4 100000\n1 2\n2 3\n3 4\n4 5",
"output": "1"
},
{
"input": "2 1 100000\n1 2",
"output": "1"
},
{
"input": "1 0 10",
"output": "1"
},
{
"input": "3 3 100\n1 2\n2 3\n3 1",
"output": "1"
},
{
"input": "2 1 100\n1 2",
"output": "1"
},
{
"input": "3 2 42\n1 2\n2 3",
"output": "1"
}
] | 62 | 0 | 0 | 1,742 |
|
670 | Holidays | [
"brute force",
"constructive algorithms",
"greedy",
"math"
] | null | null | On the planet Mars a year lasts exactly *n* days (there are no leap years on Mars). But Martians have the same weeks as earthlingsΒ β 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars. | The first line of the input contains a positive integer *n* (1<=β€<=*n*<=β€<=1<=000<=000)Β β the number of days in a year on Mars. | Print two integersΒ β the minimum possible and the maximum possible number of days off per year on Mars. | [
"14\n",
"2\n"
] | [
"4 4\n",
"0 2\n"
] | In the first sample there are 14 days in a year on Mars, and therefore independently of the day a year starts with there will be exactly 4 days off .
In the second sample there are only 2 days in a year on Mars, and they can both be either work days or days off. | [
{
"input": "14",
"output": "4 4"
},
{
"input": "2",
"output": "0 2"
},
{
"input": "1",
"output": "0 1"
},
{
"input": "3",
"output": "0 2"
},
{
"input": "4",
"output": "0 2"
},
{
"input": "5",
"output": "0 2"
},
{
"input": "6",
"output": "1 2"
},
{
"input": "7",
"output": "2 2"
},
{
"input": "8",
"output": "2 3"
},
{
"input": "9",
"output": "2 4"
},
{
"input": "10",
"output": "2 4"
},
{
"input": "11",
"output": "2 4"
},
{
"input": "12",
"output": "2 4"
},
{
"input": "13",
"output": "3 4"
},
{
"input": "1000000",
"output": "285714 285715"
},
{
"input": "16",
"output": "4 6"
},
{
"input": "17",
"output": "4 6"
},
{
"input": "18",
"output": "4 6"
},
{
"input": "19",
"output": "4 6"
},
{
"input": "20",
"output": "5 6"
},
{
"input": "21",
"output": "6 6"
},
{
"input": "22",
"output": "6 7"
},
{
"input": "23",
"output": "6 8"
},
{
"input": "24",
"output": "6 8"
},
{
"input": "25",
"output": "6 8"
},
{
"input": "26",
"output": "6 8"
},
{
"input": "27",
"output": "7 8"
},
{
"input": "28",
"output": "8 8"
},
{
"input": "29",
"output": "8 9"
},
{
"input": "30",
"output": "8 10"
},
{
"input": "100",
"output": "28 30"
},
{
"input": "99",
"output": "28 29"
},
{
"input": "98",
"output": "28 28"
},
{
"input": "97",
"output": "27 28"
},
{
"input": "96",
"output": "26 28"
},
{
"input": "95",
"output": "26 28"
},
{
"input": "94",
"output": "26 28"
},
{
"input": "93",
"output": "26 28"
},
{
"input": "92",
"output": "26 27"
},
{
"input": "91",
"output": "26 26"
},
{
"input": "90",
"output": "25 26"
},
{
"input": "89",
"output": "24 26"
},
{
"input": "88",
"output": "24 26"
},
{
"input": "87",
"output": "24 26"
},
{
"input": "86",
"output": "24 26"
},
{
"input": "85",
"output": "24 25"
},
{
"input": "84",
"output": "24 24"
},
{
"input": "83",
"output": "23 24"
},
{
"input": "82",
"output": "22 24"
},
{
"input": "81",
"output": "22 24"
},
{
"input": "80",
"output": "22 24"
},
{
"input": "1000",
"output": "285 286"
},
{
"input": "999",
"output": "284 286"
},
{
"input": "998",
"output": "284 286"
},
{
"input": "997",
"output": "284 286"
},
{
"input": "996",
"output": "284 286"
},
{
"input": "995",
"output": "284 285"
},
{
"input": "994",
"output": "284 284"
},
{
"input": "993",
"output": "283 284"
},
{
"input": "992",
"output": "282 284"
},
{
"input": "991",
"output": "282 284"
},
{
"input": "990",
"output": "282 284"
},
{
"input": "989",
"output": "282 284"
},
{
"input": "988",
"output": "282 283"
},
{
"input": "987",
"output": "282 282"
},
{
"input": "986",
"output": "281 282"
},
{
"input": "985",
"output": "280 282"
},
{
"input": "984",
"output": "280 282"
},
{
"input": "983",
"output": "280 282"
},
{
"input": "982",
"output": "280 282"
},
{
"input": "981",
"output": "280 281"
},
{
"input": "980",
"output": "280 280"
},
{
"input": "10000",
"output": "2856 2858"
},
{
"input": "9999",
"output": "2856 2858"
},
{
"input": "9998",
"output": "2856 2858"
},
{
"input": "9997",
"output": "2856 2857"
},
{
"input": "9996",
"output": "2856 2856"
},
{
"input": "9995",
"output": "2855 2856"
},
{
"input": "9994",
"output": "2854 2856"
},
{
"input": "9993",
"output": "2854 2856"
},
{
"input": "9992",
"output": "2854 2856"
},
{
"input": "9991",
"output": "2854 2856"
},
{
"input": "9990",
"output": "2854 2855"
},
{
"input": "9989",
"output": "2854 2854"
},
{
"input": "9988",
"output": "2853 2854"
},
{
"input": "9987",
"output": "2852 2854"
},
{
"input": "9986",
"output": "2852 2854"
},
{
"input": "9985",
"output": "2852 2854"
},
{
"input": "9984",
"output": "2852 2854"
},
{
"input": "9983",
"output": "2852 2853"
},
{
"input": "9982",
"output": "2852 2852"
},
{
"input": "9981",
"output": "2851 2852"
},
{
"input": "9980",
"output": "2850 2852"
},
{
"input": "100000",
"output": "28570 28572"
},
{
"input": "99999",
"output": "28570 28572"
},
{
"input": "99998",
"output": "28570 28572"
},
{
"input": "99997",
"output": "28570 28572"
},
{
"input": "99996",
"output": "28570 28571"
},
{
"input": "99995",
"output": "28570 28570"
},
{
"input": "99994",
"output": "28569 28570"
},
{
"input": "99993",
"output": "28568 28570"
},
{
"input": "99992",
"output": "28568 28570"
},
{
"input": "99991",
"output": "28568 28570"
},
{
"input": "99990",
"output": "28568 28570"
},
{
"input": "99989",
"output": "28568 28569"
},
{
"input": "99988",
"output": "28568 28568"
},
{
"input": "99987",
"output": "28567 28568"
},
{
"input": "99986",
"output": "28566 28568"
},
{
"input": "99985",
"output": "28566 28568"
},
{
"input": "99984",
"output": "28566 28568"
},
{
"input": "99983",
"output": "28566 28568"
},
{
"input": "99982",
"output": "28566 28567"
},
{
"input": "99981",
"output": "28566 28566"
},
{
"input": "99980",
"output": "28565 28566"
},
{
"input": "999999",
"output": "285714 285714"
},
{
"input": "999998",
"output": "285713 285714"
},
{
"input": "999997",
"output": "285712 285714"
},
{
"input": "999996",
"output": "285712 285714"
},
{
"input": "999995",
"output": "285712 285714"
},
{
"input": "999994",
"output": "285712 285714"
},
{
"input": "999993",
"output": "285712 285713"
},
{
"input": "999992",
"output": "285712 285712"
},
{
"input": "999991",
"output": "285711 285712"
},
{
"input": "999990",
"output": "285710 285712"
},
{
"input": "999989",
"output": "285710 285712"
},
{
"input": "999988",
"output": "285710 285712"
},
{
"input": "999987",
"output": "285710 285712"
},
{
"input": "999986",
"output": "285710 285711"
},
{
"input": "999985",
"output": "285710 285710"
},
{
"input": "999984",
"output": "285709 285710"
},
{
"input": "999983",
"output": "285708 285710"
},
{
"input": "999982",
"output": "285708 285710"
},
{
"input": "999981",
"output": "285708 285710"
},
{
"input": "999980",
"output": "285708 285710"
},
{
"input": "234123",
"output": "66892 66893"
},
{
"input": "234122",
"output": "66892 66892"
},
{
"input": "234121",
"output": "66891 66892"
},
{
"input": "234120",
"output": "66890 66892"
},
{
"input": "234119",
"output": "66890 66892"
},
{
"input": "234118",
"output": "66890 66892"
},
{
"input": "234117",
"output": "66890 66892"
},
{
"input": "234116",
"output": "66890 66891"
},
{
"input": "234115",
"output": "66890 66890"
},
{
"input": "234114",
"output": "66889 66890"
},
{
"input": "234113",
"output": "66888 66890"
},
{
"input": "234112",
"output": "66888 66890"
},
{
"input": "234111",
"output": "66888 66890"
},
{
"input": "234110",
"output": "66888 66890"
},
{
"input": "234109",
"output": "66888 66889"
},
{
"input": "234108",
"output": "66888 66888"
},
{
"input": "234107",
"output": "66887 66888"
},
{
"input": "234106",
"output": "66886 66888"
},
{
"input": "234105",
"output": "66886 66888"
},
{
"input": "234104",
"output": "66886 66888"
},
{
"input": "234103",
"output": "66886 66888"
},
{
"input": "868531",
"output": "248151 248152"
},
{
"input": "868530",
"output": "248150 248152"
},
{
"input": "868529",
"output": "248150 248152"
},
{
"input": "868528",
"output": "248150 248152"
},
{
"input": "868527",
"output": "248150 248152"
},
{
"input": "868526",
"output": "248150 248151"
},
{
"input": "868525",
"output": "248150 248150"
},
{
"input": "868524",
"output": "248149 248150"
},
{
"input": "868523",
"output": "248148 248150"
},
{
"input": "868522",
"output": "248148 248150"
},
{
"input": "868521",
"output": "248148 248150"
},
{
"input": "868520",
"output": "248148 248150"
},
{
"input": "868519",
"output": "248148 248149"
},
{
"input": "868518",
"output": "248148 248148"
},
{
"input": "868517",
"output": "248147 248148"
},
{
"input": "868516",
"output": "248146 248148"
},
{
"input": "868515",
"output": "248146 248148"
},
{
"input": "868514",
"output": "248146 248148"
},
{
"input": "868513",
"output": "248146 248148"
},
{
"input": "868512",
"output": "248146 248147"
},
{
"input": "868511",
"output": "248146 248146"
},
{
"input": "123413",
"output": "35260 35262"
},
{
"input": "123412",
"output": "35260 35262"
},
{
"input": "123411",
"output": "35260 35261"
},
{
"input": "123410",
"output": "35260 35260"
},
{
"input": "123409",
"output": "35259 35260"
},
{
"input": "123408",
"output": "35258 35260"
},
{
"input": "123407",
"output": "35258 35260"
},
{
"input": "123406",
"output": "35258 35260"
},
{
"input": "123405",
"output": "35258 35260"
},
{
"input": "123404",
"output": "35258 35259"
},
{
"input": "123403",
"output": "35258 35258"
},
{
"input": "123402",
"output": "35257 35258"
},
{
"input": "123401",
"output": "35256 35258"
},
{
"input": "123400",
"output": "35256 35258"
},
{
"input": "123399",
"output": "35256 35258"
},
{
"input": "123398",
"output": "35256 35258"
},
{
"input": "123397",
"output": "35256 35257"
},
{
"input": "123396",
"output": "35256 35256"
},
{
"input": "123395",
"output": "35255 35256"
},
{
"input": "123394",
"output": "35254 35256"
},
{
"input": "123393",
"output": "35254 35256"
},
{
"input": "15",
"output": "4 5"
}
] | 140 | 0 | 0 | 1,747 |
|
997 | Convert to Ones | [
"brute force",
"greedy",
"implementation",
"math"
] | null | null | You've got a string $a_1, a_2, \dots, a_n$, consisting of zeros and ones.
Let's call a sequence of consecutive elements $a_i, a_{i<=+<=1}, \ldots,<=a_j$ ($1\leq<=i\leq<=j\leq<=n$) a substring of string $a$.
You can apply the following operations any number of times:
- Choose some substring of string $a$ (for example, you can choose entire string) and reverse it, paying $x$ coins for it (for example, Β«0101101Β» $\to$ Β«0111001Β»); - Choose some substring of string $a$ (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and onesΒ β by zeros), paying $y$ coins for it (for example, Β«0101101Β» $\to$ Β«0110001Β»).
You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring.
What is the minimum number of coins you need to spend to get a string consisting only of ones? | The first line of input contains integers $n$, $x$ and $y$ ($1<=\leq<=n<=\leq<=300\,000, 0 \leq x, y \leq 10^9$)Β β length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring).
The second line contains the string $a$ of length $n$, consisting of zeros and ones. | Print a single integerΒ β the minimum total cost of operations you need to spend to get a string consisting only of ones. Print $0$, if you do not need to perform any operations. | [
"5 1 10\n01000\n",
"5 10 1\n01000\n",
"7 2 3\n1111111\n"
] | [
"11\n",
"2\n",
"0\n"
] | In the first sample, at first you need to reverse substring $[1 \dots 2]$, and then you need to invert substring $[2 \dots 5]$.
Then the string was changed as follows:
Β«01000Β» $\to$ Β«10000Β» $\to$ Β«11111Β».
The total cost of operations is $1 + 10 = 11$.
In the second sample, at first you need to invert substring $[1 \dots 1]$, and then you need to invert substring $[3 \dots 5]$.
Then the string was changed as follows:
Β«01000Β» $\to$ Β«11000Β» $\to$ Β«11111Β».
The overall cost is $1 + 1 = 2$.
In the third example, string already consists only of ones, so the answer is $0$. | [
{
"input": "5 1 10\n01000",
"output": "11"
},
{
"input": "5 10 1\n01000",
"output": "2"
},
{
"input": "7 2 3\n1111111",
"output": "0"
},
{
"input": "1 60754033 959739508\n0",
"output": "959739508"
},
{
"input": "1 431963980 493041212\n1",
"output": "0"
},
{
"input": "1 314253869 261764879\n0",
"output": "261764879"
},
{
"input": "1 491511050 399084767\n1",
"output": "0"
},
{
"input": "2 163093925 214567542\n00",
"output": "214567542"
},
{
"input": "2 340351106 646854722\n10",
"output": "646854722"
},
{
"input": "2 222640995 489207317\n01",
"output": "489207317"
},
{
"input": "2 399898176 552898277\n11",
"output": "0"
},
{
"input": "2 690218164 577155357\n00",
"output": "577155357"
},
{
"input": "2 827538051 754412538\n10",
"output": "754412538"
},
{
"input": "2 636702427 259825230\n01",
"output": "259825230"
},
{
"input": "2 108926899 102177825\n11",
"output": "0"
},
{
"input": "3 368381052 440077270\n000",
"output": "440077270"
},
{
"input": "3 505700940 617334451\n100",
"output": "617334451"
},
{
"input": "3 499624340 643020827\n010",
"output": "1142645167"
},
{
"input": "3 75308005 971848814\n110",
"output": "971848814"
},
{
"input": "3 212627893 854138703\n001",
"output": "854138703"
},
{
"input": "3 31395883 981351561\n101",
"output": "981351561"
},
{
"input": "3 118671447 913685773\n011",
"output": "913685773"
},
{
"input": "3 255991335 385910245\n111",
"output": "0"
},
{
"input": "3 688278514 268200134\n000",
"output": "268200134"
},
{
"input": "3 825598402 445457315\n100",
"output": "445457315"
},
{
"input": "3 300751942 45676507\n010",
"output": "91353014"
},
{
"input": "3 517900980 438071829\n110",
"output": "438071829"
},
{
"input": "3 400190869 280424424\n001",
"output": "280424424"
},
{
"input": "3 577448050 344115384\n101",
"output": "344115384"
},
{
"input": "3 481435271 459737939\n011",
"output": "459737939"
},
{
"input": "3 931962412 913722450\n111",
"output": "0"
},
{
"input": "4 522194562 717060616\n0000",
"output": "717060616"
},
{
"input": "4 659514449 894317797\n1000",
"output": "894317797"
},
{
"input": "4 71574977 796834337\n0100",
"output": "868409314"
},
{
"input": "4 248832158 934154224\n1100",
"output": "934154224"
},
{
"input": "4 71474110 131122047\n0010",
"output": "202596157"
},
{
"input": "4 308379228 503761290\n1010",
"output": "812140518"
},
{
"input": "4 272484957 485636409\n0110",
"output": "758121366"
},
{
"input": "4 662893590 704772137\n1110",
"output": "704772137"
},
{
"input": "4 545183479 547124732\n0001",
"output": "547124732"
},
{
"input": "4 684444619 722440661\n1001",
"output": "722440661"
},
{
"input": "4 477963686 636258459\n0101",
"output": "1114222145"
},
{
"input": "4 360253575 773578347\n1101",
"output": "773578347"
},
{
"input": "4 832478048 910898234\n0011",
"output": "910898234"
},
{
"input": "4 343185412 714767937\n1011",
"output": "714767937"
},
{
"input": "4 480505300 892025118\n0111",
"output": "892025118"
},
{
"input": "4 322857895 774315007\n1111",
"output": "0"
},
{
"input": "4 386548854 246539479\n0000",
"output": "246539479"
},
{
"input": "4 523868742 128829368\n1000",
"output": "128829368"
},
{
"input": "4 956155921 11119257\n0100",
"output": "22238514"
},
{
"input": "4 188376438 93475808\n1100",
"output": "93475808"
},
{
"input": "4 754947032 158668188\n0010",
"output": "317336376"
},
{
"input": "4 927391856 637236921\n1010",
"output": "1274473842"
},
{
"input": "4 359679035 109461393\n0110",
"output": "218922786"
},
{
"input": "4 991751283 202031630\n1110",
"output": "202031630"
},
{
"input": "4 339351517 169008463\n0001",
"output": "169008463"
},
{
"input": "4 771638697 346265644\n1001",
"output": "346265644"
},
{
"input": "4 908958584 523522825\n0101",
"output": "1047045650"
},
{
"input": "4 677682252 405812714\n1101",
"output": "405812714"
},
{
"input": "4 815002139 288102603\n0011",
"output": "288102603"
},
{
"input": "4 952322026 760327076\n1011",
"output": "760327076"
},
{
"input": "4 663334158 312481698\n0111",
"output": "312481698"
},
{
"input": "4 840591339 154834293\n1111",
"output": "0"
},
{
"input": "14 3 11\n10110100011001",
"output": "20"
},
{
"input": "19 1 1\n1010101010101010101",
"output": "9"
},
{
"input": "1 10 1\n1",
"output": "0"
},
{
"input": "1 100 1\n1",
"output": "0"
},
{
"input": "5 1000 1\n11111",
"output": "0"
},
{
"input": "5 10 1\n11111",
"output": "0"
},
{
"input": "7 3 2\n1111111",
"output": "0"
},
{
"input": "5 1 10\n10101",
"output": "11"
},
{
"input": "1 3 2\n1",
"output": "0"
},
{
"input": "2 10 1\n11",
"output": "0"
},
{
"input": "4 148823922 302792601\n1010",
"output": "451616523"
},
{
"input": "1 2 1\n1",
"output": "0"
},
{
"input": "5 2 3\n00011",
"output": "3"
},
{
"input": "1 5 0\n1",
"output": "0"
},
{
"input": "7 2 3\n1001001",
"output": "5"
},
{
"input": "10 1 1000000000\n1111010111",
"output": "1000000001"
},
{
"input": "25 999999998 999999999\n1011001110101010100111001",
"output": "7999999985"
},
{
"input": "2 0 1\n00",
"output": "1"
},
{
"input": "2 1 100\n10",
"output": "100"
},
{
"input": "7 20 3\n1111111",
"output": "0"
},
{
"input": "1 1 0\n1",
"output": "0"
},
{
"input": "3 1 10\n010",
"output": "11"
},
{
"input": "2 1 0\n11",
"output": "0"
},
{
"input": "7 100 3\n1111111",
"output": "0"
},
{
"input": "5 1 1000\n10101",
"output": "1001"
},
{
"input": "5 2 1\n11111",
"output": "0"
},
{
"input": "1 1000 1\n1",
"output": "0"
},
{
"input": "1 799543940 488239239\n1",
"output": "0"
},
{
"input": "6 1 1000\n010101",
"output": "1002"
},
{
"input": "5 11 1\n11111",
"output": "0"
},
{
"input": "5 2 3\n10101",
"output": "5"
},
{
"input": "3 10 1\n111",
"output": "0"
},
{
"input": "7 9 10\n1001011",
"output": "19"
},
{
"input": "5 5 6\n10101",
"output": "11"
},
{
"input": "1 1000000000 0\n1",
"output": "0"
},
{
"input": "4 0 1\n0101",
"output": "1"
},
{
"input": "8 2 3\n10101010",
"output": "9"
},
{
"input": "6 3 100\n010101",
"output": "106"
},
{
"input": "3 3 2\n111",
"output": "0"
},
{
"input": "1 20 1\n1",
"output": "0"
},
{
"input": "2 1 2\n01",
"output": "2"
}
] | 389 | 77,619,200 | 3 | 1,750 |
|
982 | Row | [
"brute force",
"constructive algorithms"
] | null | null | You're given a row with $n$ chairs. We call a seating of people "maximal" if the two following conditions hold:
1. There are no neighbors adjacent to anyone seated. 1. It's impossible to seat one more person without violating the first rule.
The seating is given as a string consisting of zeros and ones ($0$ means that the corresponding seat is empty, $1$ β occupied). The goal is to determine whether this seating is "maximal".
Note that the first and last seats are not adjacent (if $n \ne 2$). | The first line contains a single integer $n$ ($1 \leq n \leq 1000$)Β β the number of chairs.
The next line contains a string of $n$ characters, each of them is either zero or one, describing the seating. | Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No".
You are allowed to print letters in whatever case you'd like (uppercase or lowercase). | [
"3\n101\n",
"4\n1011\n",
"5\n10001\n"
] | [
"Yes\n",
"No\n",
"No\n"
] | In sample case one the given seating is maximal.
In sample case two the person at chair three has a neighbour to the right.
In sample case three it is possible to seat yet another person into chair three. | [
{
"input": "3\n101",
"output": "Yes"
},
{
"input": "4\n1011",
"output": "No"
},
{
"input": "5\n10001",
"output": "No"
},
{
"input": "1\n0",
"output": "No"
},
{
"input": "1\n1",
"output": "Yes"
},
{
"input": "100\n0101001010101001010010010101001010100101001001001010010101010010101001001010101001001001010100101010",
"output": "Yes"
},
{
"input": "4\n0100",
"output": "No"
},
{
"input": "42\n011000100101001001101011011010100010011010",
"output": "No"
},
{
"input": "3\n001",
"output": "No"
},
{
"input": "64\n1001001010010010100101010010010100100101001001001001010100101001",
"output": "Yes"
},
{
"input": "3\n111",
"output": "No"
},
{
"input": "4\n0000",
"output": "No"
},
{
"input": "4\n0001",
"output": "No"
},
{
"input": "4\n0010",
"output": "No"
},
{
"input": "4\n0011",
"output": "No"
},
{
"input": "4\n0101",
"output": "Yes"
},
{
"input": "4\n0110",
"output": "No"
},
{
"input": "4\n0111",
"output": "No"
},
{
"input": "4\n1000",
"output": "No"
},
{
"input": "4\n1001",
"output": "Yes"
},
{
"input": "4\n1010",
"output": "Yes"
},
{
"input": "4\n1100",
"output": "No"
},
{
"input": "4\n1101",
"output": "No"
},
{
"input": "4\n1110",
"output": "No"
},
{
"input": "4\n1111",
"output": "No"
},
{
"input": "2\n00",
"output": "No"
},
{
"input": "2\n01",
"output": "Yes"
},
{
"input": "2\n10",
"output": "Yes"
},
{
"input": "2\n11",
"output": "No"
},
{
"input": "3\n000",
"output": "No"
},
{
"input": "3\n010",
"output": "Yes"
},
{
"input": "3\n011",
"output": "No"
},
{
"input": "3\n100",
"output": "No"
},
{
"input": "3\n110",
"output": "No"
},
{
"input": "100\n0111001010101110001100000010011000100101110010001100111110101110001110101010111000010010011000000110",
"output": "No"
},
{
"input": "357\n100101010010010010010100101001001010101010100100100100101001010101001010010100101001010100101001010010100100101001010101010101001001010100101010010100101001010100100100101010010010010100101010010010101001010010010101001001010010010101010100100101010010100100101001010100101001010100101001010010010010100101001010100100100100100100100101010101010010010100101",
"output": "Yes"
},
{
"input": "459\n000111000101010000100001001010111110011011010001100101111010111011101110111101111101100101100011011001100110001001111001101000111001011100110100011111011111000010000110010011100110011011111110011100001101001111000100111011001000001011111100110100001001001100101011100001110110100101011011110100100111101011000101110000100110100100010000000100001001111111000011101010010011001111010111001100000100111001010111011010000011000011100101101011101000011011000110011",
"output": "No"
},
{
"input": "3\n001",
"output": "No"
},
{
"input": "4\n0010",
"output": "No"
},
{
"input": "3\n111",
"output": "No"
},
{
"input": "4\n0110",
"output": "No"
},
{
"input": "2\n11",
"output": "No"
},
{
"input": "2\n10",
"output": "Yes"
},
{
"input": "2\n01",
"output": "Yes"
},
{
"input": "4\n1101",
"output": "No"
},
{
"input": "2\n00",
"output": "No"
},
{
"input": "4\n1111",
"output": "No"
},
{
"input": "15\n000010101010000",
"output": "No"
},
{
"input": "8\n10010100",
"output": "No"
},
{
"input": "5\n00100",
"output": "No"
},
{
"input": "6\n100100",
"output": "No"
},
{
"input": "7\n1000001",
"output": "No"
},
{
"input": "98\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "Yes"
}
] | 77 | 0 | 0 | 1,753 |
|
988 | Equal Sums | [
"implementation",
"sortings"
] | null | null | You are given $k$ sequences of integers. The length of the $i$-th sequence equals to $n_i$.
You have to choose exactly two sequences $i$ and $j$ ($i \ne j$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $i$ (its length will be equal to $n_i - 1$) equals to the sum of the changed sequence $j$ (its length will be equal to $n_j - 1$).
Note that it's required to remove exactly one element in each of the two chosen sequences.
Assume that the sum of the empty (of the length equals $0$) sequence is $0$. | The first line contains an integer $k$ ($2 \le k \le 2 \cdot 10^5$) β the number of sequences.
Then $k$ pairs of lines follow, each pair containing a sequence.
The first line in the $i$-th pair contains one integer $n_i$ ($1 \le n_i < 2 \cdot 10^5$) β the length of the $i$-th sequence. The second line of the $i$-th pair contains a sequence of $n_i$ integers $a_{i, 1}, a_{i, 2}, \dots, a_{i, n_i}$.
The elements of sequences are integer numbers from $-10^4$ to $10^4$.
The sum of lengths of all given sequences don't exceed $2 \cdot 10^5$, i.e. $n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5$. | If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line β two integers $i$, $x$ ($1 \le i \le k, 1 \le x \le n_i$), in the third line β two integers $j$, $y$ ($1 \le j \le k, 1 \le y \le n_j$). It means that the sum of the elements of the $i$-th sequence without the element with index $x$ equals to the sum of the elements of the $j$-th sequence without the element with index $y$.
Two chosen sequences must be distinct, i.e. $i \ne j$. You can print them in any order.
If there are multiple possible answers, print any of them. | [
"2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1\n",
"3\n1\n5\n5\n1 1 1 1 1\n2\n2 3\n",
"4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2\n"
] | [
"YES\n2 6\n1 2\n",
"NO\n",
"YES\n2 2\n4 1\n"
] | In the first example there are two sequences $[2, 3, 1, 3, 2]$ and $[1, 1, 2, 2, 2, 1]$. You can remove the second element from the first sequence to get $[2, 1, 3, 2]$ and you can remove the sixth element from the second sequence to get $[1, 1, 2, 2, 2]$. The sums of the both resulting sequences equal to $8$, i.e. the sums are equal. | [
{
"input": "2\n5\n2 3 1 3 2\n6\n1 1 2 2 2 1",
"output": "YES\n2 1\n1 4"
},
{
"input": "3\n1\n5\n5\n1 1 1 1 1\n2\n2 3",
"output": "NO"
},
{
"input": "4\n6\n2 2 2 2 2 2\n5\n2 2 2 2 2\n3\n2 2 2\n5\n2 2 2 2 2",
"output": "YES\n4 1\n2 5"
},
{
"input": "2\n2\n0 -10000\n2\n10000 0",
"output": "YES\n2 1\n1 2"
}
] | 2,000 | 29,798,400 | 0 | 1,756 |
|
129 | Cookies | [
"implementation"
] | null | null | Olga came to visit the twins Anna and Maria and saw that they have many cookies. The cookies are distributed into bags. As there are many cookies, Olga decided that it's no big deal if she steals a bag. However, she doesn't want the sisters to quarrel because of nothing when they divide the cookies. That's why Olga wants to steal a bag with cookies so that the number of cookies in the remaining bags was even, that is, so that Anna and Maria could evenly divide it into two (even 0 remaining cookies will do, just as any other even number). How many ways there are to steal exactly one cookie bag so that the total number of cookies in the remaining bags was even? | The first line contains the only integer *n* (1<=β€<=*n*<=β€<=100) β the number of cookie bags Anna and Maria have. The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=100) β the number of cookies in the *i*-th bag. | Print in the only line the only number β the sought number of ways. If there are no such ways print 0. | [
"1\n1\n",
"10\n1 2 2 3 4 4 4 2 2 2\n",
"11\n2 2 2 2 2 2 2 2 2 2 99\n"
] | [
"1\n",
"8\n",
"1\n"
] | In the first sample Olga should take the only bag so that the twins ended up with the even number of cookies.
In the second sample Olga can take any of five bags with two cookies or any of three bags with four cookies β 5β+β3β=β8 ways in total.
In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2β*β9β+β99β=β117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies. | [
{
"input": "1\n1",
"output": "1"
},
{
"input": "10\n1 2 2 3 4 4 4 2 2 2",
"output": "8"
},
{
"input": "11\n2 2 2 2 2 2 2 2 2 2 99",
"output": "1"
},
{
"input": "2\n1 1",
"output": "0"
},
{
"input": "2\n2 2",
"output": "2"
},
{
"input": "2\n1 2",
"output": "1"
},
{
"input": "7\n7 7 7 7 7 7 7",
"output": "7"
},
{
"input": "8\n1 2 3 4 5 6 7 8",
"output": "4"
},
{
"input": "100\n1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 2",
"output": "50"
},
{
"input": "99\n99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99 100 99",
"output": "49"
},
{
"input": "82\n43 44 96 33 23 42 33 66 53 87 8 90 43 91 40 88 51 18 48 62 59 10 22 20 54 6 13 63 2 56 31 52 98 42 54 32 26 77 9 24 33 91 16 30 39 34 78 82 73 90 12 15 67 76 30 18 44 86 84 98 65 54 100 79 28 34 40 56 11 43 72 35 86 59 89 40 30 33 7 19 44 15",
"output": "50"
},
{
"input": "17\n50 14 17 77 74 74 38 76 41 27 45 29 66 98 38 73 38",
"output": "7"
},
{
"input": "94\n81 19 90 99 26 11 86 44 78 36 80 59 99 90 78 72 71 20 94 56 42 40 71 84 10 85 10 70 52 27 39 55 90 16 48 25 7 79 99 100 38 10 99 56 3 4 78 9 16 57 14 40 52 54 57 70 30 86 56 84 97 60 59 69 49 66 23 92 90 46 86 73 53 47 1 83 14 20 24 66 13 45 41 14 86 75 55 88 48 95 82 24 47 87",
"output": "39"
},
{
"input": "88\n64 95 12 90 40 65 98 45 52 54 79 7 81 25 98 19 68 82 41 53 35 50 5 22 32 21 8 39 8 6 72 27 81 30 12 79 21 42 60 2 66 87 46 93 62 78 52 71 76 32 78 94 86 85 55 15 34 76 41 20 32 26 94 81 89 45 74 49 11 40 40 39 49 46 80 85 90 23 80 40 86 58 70 26 48 93 23 53",
"output": "37"
},
{
"input": "84\n95 9 43 43 13 84 60 90 1 8 97 99 54 34 59 83 33 15 51 26 40 12 66 65 19 30 29 78 92 60 25 13 19 84 71 73 12 24 54 49 16 41 11 40 57 59 34 40 39 9 71 83 1 77 79 53 94 47 78 55 77 85 29 52 80 90 53 77 97 97 27 79 28 23 83 25 26 22 49 86 63 56 3 32",
"output": "51"
},
{
"input": "47\n61 97 76 94 91 22 2 68 62 73 90 47 16 79 44 71 98 68 43 6 53 52 40 27 68 67 43 96 14 91 60 61 96 24 97 13 32 65 85 96 81 77 34 18 23 14 80",
"output": "21"
},
{
"input": "69\n71 1 78 74 58 89 30 6 100 90 22 61 11 59 14 74 27 25 78 61 45 19 25 33 37 4 52 43 53 38 9 100 56 67 69 38 76 91 63 60 93 52 28 61 9 98 8 14 57 63 89 64 98 51 36 66 36 86 13 82 50 91 52 64 86 78 78 83 81",
"output": "37"
},
{
"input": "52\n38 78 36 75 19 3 56 1 39 97 24 79 84 16 93 55 96 64 12 24 1 86 80 29 12 32 36 36 73 39 76 65 53 98 30 20 28 8 86 43 70 22 75 69 62 65 81 25 53 40 71 59",
"output": "28"
},
{
"input": "74\n81 31 67 97 26 75 69 81 11 13 13 74 77 88 52 20 52 64 66 75 72 28 41 54 26 75 41 91 75 15 18 36 13 83 63 61 14 48 53 63 19 67 35 48 23 65 73 100 44 55 92 88 99 17 73 25 83 7 31 89 12 80 98 39 42 75 14 29 81 35 77 87 33 94",
"output": "47"
},
{
"input": "44\n46 56 31 31 37 71 94 2 14 100 45 72 36 72 80 3 38 54 42 98 50 32 31 42 62 31 45 50 95 100 18 17 64 22 18 25 52 56 70 57 43 40 81 28",
"output": "15"
},
{
"input": "22\n28 57 40 74 51 4 45 84 99 12 95 14 92 60 47 81 84 51 31 91 59 42",
"output": "11"
},
{
"input": "59\n73 45 94 76 41 49 65 13 74 66 36 25 47 75 40 23 92 72 11 32 32 8 81 26 68 56 41 8 76 47 96 55 70 11 84 14 83 18 70 22 30 39 28 100 48 11 92 45 78 69 86 1 54 90 98 91 13 17 35",
"output": "33"
},
{
"input": "63\n20 18 44 94 68 57 16 43 74 55 68 24 21 95 76 84 50 50 47 86 86 12 58 55 28 72 86 18 34 45 81 88 3 72 41 9 60 90 81 93 12 6 9 6 2 41 1 7 9 29 81 14 64 80 20 36 67 54 7 5 35 81 22",
"output": "37"
},
{
"input": "28\n49 84 48 19 44 91 11 82 96 95 88 90 71 82 87 25 31 23 18 13 98 45 26 65 35 12 31 14",
"output": "15"
},
{
"input": "61\n34 18 28 64 28 45 9 77 77 20 63 92 79 16 16 100 86 2 91 91 57 15 31 95 10 88 84 5 82 83 53 98 59 17 97 80 76 80 81 3 91 81 87 93 61 46 10 49 6 22 21 75 63 89 21 81 30 19 67 38 77",
"output": "35"
},
{
"input": "90\n41 90 43 1 28 75 90 50 3 70 76 64 81 63 25 69 83 82 29 91 59 66 21 61 7 55 72 49 38 69 72 20 64 58 30 81 61 29 96 14 39 5 100 20 29 98 75 29 44 78 97 45 26 77 73 59 22 99 41 6 3 96 71 20 9 18 96 18 90 62 34 78 54 5 41 6 73 33 2 54 26 21 18 6 45 57 43 73 95 75",
"output": "42"
},
{
"input": "45\n93 69 4 27 20 14 71 48 79 3 32 26 49 30 57 88 13 56 49 61 37 32 47 41 41 70 45 68 82 18 8 6 25 20 15 13 71 99 28 6 52 34 19 59 26",
"output": "23"
},
{
"input": "33\n29 95 48 49 91 10 83 71 47 25 66 36 51 12 34 10 54 74 41 96 89 26 89 1 42 33 1 62 9 32 49 65 78",
"output": "15"
},
{
"input": "34\n98 24 42 36 41 82 28 58 89 34 77 70 76 44 74 54 66 100 13 79 4 88 21 1 11 45 91 29 87 100 29 54 82 78",
"output": "13"
},
{
"input": "29\n91 84 26 84 9 63 52 9 65 56 90 2 36 7 67 33 91 14 65 38 53 36 81 83 85 14 33 95 51",
"output": "17"
},
{
"input": "100\n2 88 92 82 87 100 78 28 84 43 78 32 43 33 97 19 15 52 29 84 57 72 54 13 99 28 82 79 40 70 34 92 91 53 9 88 27 43 14 92 72 37 26 37 20 95 19 34 49 64 33 37 34 27 80 79 9 54 99 68 25 4 68 73 46 66 24 78 3 87 26 52 50 84 4 95 23 83 39 58 86 36 33 16 98 2 84 19 53 12 69 60 10 11 78 17 79 92 77 59",
"output": "45"
},
{
"input": "100\n2 95 45 73 9 54 20 97 57 82 88 26 18 71 25 27 75 54 31 11 58 85 69 75 72 91 76 5 25 80 45 49 4 73 8 81 81 38 5 12 53 77 7 96 90 35 28 80 73 94 19 69 96 17 94 49 69 9 32 19 5 12 46 29 26 40 59 59 6 95 82 50 72 2 45 69 12 5 72 29 39 72 23 96 81 28 28 56 68 58 37 41 30 1 90 84 15 24 96 43",
"output": "53"
},
{
"input": "100\n27 72 35 91 13 10 35 45 24 55 83 84 63 96 29 79 34 67 63 92 48 83 18 77 28 27 49 66 29 88 55 15 6 58 14 67 94 36 77 7 7 64 61 52 71 18 36 99 76 6 50 67 16 13 41 7 89 73 61 51 78 22 78 32 76 100 3 31 89 71 63 53 15 85 77 54 89 33 68 74 3 23 57 5 43 89 75 35 9 86 90 11 31 46 48 37 74 17 77 8",
"output": "40"
},
{
"input": "100\n69 98 69 88 11 49 55 8 25 91 17 81 47 26 15 73 96 71 18 42 42 61 48 14 92 78 35 72 4 27 62 75 83 79 17 16 46 80 96 90 82 54 37 69 85 21 67 70 96 10 46 63 21 59 56 92 54 88 77 30 75 45 44 29 86 100 51 11 65 69 66 56 82 63 27 1 51 51 13 10 3 55 26 85 34 16 87 72 13 100 81 71 90 95 86 50 83 55 55 54",
"output": "53"
},
{
"input": "100\n34 35 99 64 2 66 78 93 20 48 12 79 19 10 87 7 42 92 60 79 5 2 24 89 57 48 63 92 74 4 16 51 7 12 90 48 87 17 18 73 51 58 97 97 25 38 15 97 96 73 67 91 6 75 14 13 87 79 75 3 15 55 35 95 71 45 10 13 20 37 82 26 2 22 13 83 97 84 39 79 43 100 54 59 98 8 61 34 7 65 75 44 24 77 73 88 34 95 44 77",
"output": "55"
},
{
"input": "100\n15 86 3 1 51 26 74 85 37 87 64 58 10 6 57 26 30 47 85 65 24 72 50 40 12 35 91 47 91 60 47 87 95 34 80 91 26 3 36 39 14 86 28 70 51 44 28 21 72 79 57 61 16 71 100 94 57 67 36 74 24 21 89 85 25 2 97 67 76 53 76 80 97 64 35 13 8 32 21 52 62 61 67 14 74 73 66 44 55 76 24 3 43 42 99 61 36 80 38 66",
"output": "52"
},
{
"input": "100\n45 16 54 54 80 94 74 93 75 85 58 95 79 30 81 2 84 4 57 23 92 64 78 1 50 36 13 27 56 54 10 77 87 1 5 38 85 74 94 82 30 45 72 83 82 30 81 82 82 3 69 82 7 92 39 60 94 42 41 5 3 17 67 21 79 44 79 96 28 3 53 68 79 89 63 83 1 44 4 31 84 15 73 77 19 66 54 6 73 1 67 24 91 11 86 45 96 82 20 89",
"output": "51"
},
{
"input": "100\n84 23 50 32 90 71 92 43 58 70 6 82 7 55 85 19 70 89 12 26 29 56 74 30 2 27 4 39 63 67 91 81 11 33 75 10 82 88 39 43 43 80 68 35 55 67 53 62 73 65 86 74 43 51 14 48 42 92 83 57 22 33 24 99 5 27 78 96 7 28 11 15 8 38 85 67 5 92 24 96 57 59 14 95 91 4 9 18 45 33 74 83 64 85 14 51 51 94 29 2",
"output": "53"
},
{
"input": "100\n77 56 56 45 73 55 32 37 39 50 30 95 79 21 44 34 51 43 86 91 39 30 85 15 35 93 100 14 57 31 80 79 38 40 88 4 91 54 7 95 76 26 62 84 17 33 67 47 6 82 69 51 17 2 59 24 11 12 31 90 12 11 55 38 72 49 30 50 42 46 5 97 9 9 30 45 86 23 19 82 40 42 5 40 35 98 35 32 60 60 5 28 84 35 21 49 68 53 68 23",
"output": "48"
},
{
"input": "100\n78 38 79 61 45 86 83 83 86 90 74 69 2 84 73 39 2 5 20 71 24 80 54 89 58 34 77 40 39 62 2 47 28 53 97 75 88 98 94 96 33 71 44 90 47 36 19 89 87 98 90 87 5 85 34 79 82 3 42 88 89 63 35 7 89 30 40 48 12 41 56 76 83 60 80 80 39 56 77 4 72 96 30 55 57 51 7 19 11 1 66 1 91 87 11 62 95 85 79 25",
"output": "48"
},
{
"input": "100\n5 34 23 20 76 75 19 51 17 82 60 13 83 6 65 16 20 43 66 54 87 10 87 73 50 24 16 98 33 28 80 52 54 82 26 92 14 13 84 92 94 29 61 21 60 20 48 94 24 20 75 70 58 27 68 45 86 89 29 8 67 38 83 48 18 100 11 22 46 84 52 97 70 19 50 75 3 7 52 53 72 41 18 31 1 38 49 53 11 64 99 76 9 87 48 12 100 32 44 71",
"output": "58"
},
{
"input": "100\n76 89 68 78 24 72 73 95 98 72 58 15 2 5 56 32 9 65 50 70 94 31 29 54 89 52 31 93 43 56 26 35 72 95 51 55 78 70 11 92 17 5 54 94 81 31 78 95 73 91 95 37 59 9 53 48 65 55 84 8 45 97 64 37 96 34 36 53 66 17 72 48 99 23 27 18 92 84 44 73 60 78 53 29 68 99 19 39 61 40 69 6 77 12 47 29 15 4 8 45",
"output": "53"
},
{
"input": "100\n82 40 31 53 8 50 85 93 3 84 54 17 96 59 51 42 18 19 35 84 79 31 17 46 54 82 72 49 35 73 26 89 61 73 3 50 12 29 25 77 88 21 58 24 22 89 96 54 82 29 96 56 77 16 1 68 90 93 20 23 57 22 31 18 92 90 51 14 50 72 31 54 12 50 66 62 2 34 17 45 68 50 87 97 23 71 1 72 17 82 42 15 20 78 4 49 66 59 10 17",
"output": "54"
},
{
"input": "100\n32 82 82 24 39 53 48 5 29 24 9 37 91 37 91 95 1 97 84 52 12 56 93 47 22 20 14 17 40 22 79 34 24 2 69 30 69 29 3 89 21 46 60 92 39 29 18 24 49 18 40 22 60 13 77 50 39 64 50 70 99 8 66 31 90 38 20 54 7 21 5 56 41 68 69 20 54 89 69 62 9 53 43 89 81 97 15 2 52 78 89 65 16 61 59 42 56 25 32 52",
"output": "49"
},
{
"input": "100\n72 54 23 24 97 14 99 87 15 25 7 23 17 87 72 31 71 87 34 82 51 77 74 85 62 38 24 7 84 48 98 21 29 71 70 84 25 58 67 92 18 44 32 9 81 15 53 29 63 18 86 16 7 31 38 99 70 32 89 16 23 11 66 96 69 82 97 59 6 9 49 80 85 19 6 9 52 51 85 74 53 46 73 55 31 63 78 61 34 80 77 65 87 77 92 52 89 8 52 31",
"output": "44"
},
{
"input": "100\n56 88 8 19 7 15 11 54 35 50 19 57 63 72 51 43 50 19 57 90 40 100 8 92 11 96 30 32 59 65 93 47 62 3 50 41 30 50 72 83 61 46 83 60 20 46 33 1 5 18 83 22 34 16 41 95 63 63 7 59 55 95 91 29 64 60 64 81 45 45 10 9 88 37 69 85 21 82 41 76 42 34 47 78 51 83 65 100 13 22 59 76 63 1 26 86 36 94 99 74",
"output": "46"
},
{
"input": "100\n27 89 67 60 62 80 43 50 28 88 72 5 94 11 63 91 18 78 99 3 71 26 12 97 74 62 23 24 22 3 100 72 98 7 94 32 12 75 61 88 42 48 10 14 45 9 48 56 73 76 70 70 79 90 35 39 96 37 81 11 19 65 99 39 23 79 34 61 35 74 90 37 73 23 46 21 94 84 73 58 11 89 13 9 10 85 42 78 73 32 53 39 49 90 43 5 28 31 97 75",
"output": "53"
},
{
"input": "100\n33 24 97 96 1 14 99 51 13 65 67 20 46 88 42 44 20 49 5 89 98 83 15 40 74 83 58 3 10 79 34 2 69 28 37 100 55 52 14 8 44 94 97 89 6 42 11 28 30 33 55 56 20 57 52 25 75 1 87 42 62 41 37 12 54 85 95 80 42 36 94 96 28 76 54 36 4 17 26 24 62 15 17 79 84 36 92 78 74 91 96 77 54 92 81 91 62 98 37 37",
"output": "43"
},
{
"input": "100\n86 24 61 15 11 85 1 31 47 36 23 36 59 34 3 27 16 29 82 28 58 52 52 66 71 61 98 39 60 20 67 41 67 90 73 29 92 17 70 95 58 98 58 32 21 73 46 56 87 72 80 75 40 27 94 31 59 92 93 37 14 99 96 21 97 23 81 91 52 52 96 94 92 28 38 29 52 16 57 27 17 24 91 21 79 55 96 98 95 94 23 78 79 12 77 35 32 75 100 82",
"output": "51"
},
{
"input": "100\n88 85 41 37 69 21 7 69 36 5 92 26 64 75 22 46 67 20 70 22 62 66 38 24 47 49 68 30 90 31 67 86 86 82 9 51 43 45 48 42 73 44 31 94 45 60 54 66 20 87 11 94 34 32 87 66 56 28 75 39 37 90 72 93 55 72 31 42 30 71 87 61 4 12 12 81 23 61 56 98 71 32 30 33 96 63 92 16 8 78 47 91 47 54 49 3 81 82 41 5",
"output": "47"
},
{
"input": "1\n2",
"output": "1"
},
{
"input": "5\n1 1 3 2 2",
"output": "3"
}
] | 248 | 0 | 3 | 1,757 |
|
811 | Vladik and Courtesy | [
"brute force",
"implementation"
] | null | null | At regular competition Vladik and Valera won *a* and *b* candies respectively. Vladik offered 1 his candy to Valera. After that Valera gave Vladik 2 his candies, so that no one thought that he was less generous. Vladik for same reason gave 3 candies to Valera in next turn.
More formally, the guys take turns giving each other one candy more than they received in the previous turn.
This continued until the moment when one of them couldnβt give the right amount of candy. Candies, which guys got from each other, they donβt consider as their own. You need to know, who is the first who canβt give the right amount of candy. | Single line of input data contains two space-separated integers *a*, *b* (1<=β€<=*a*,<=*b*<=β€<=109) β number of Vladik and Valera candies respectively. | Pring a single line "Vladikββ in case, if Vladik first who canβt give right amount of candy, or "Valeraββ otherwise. | [
"1 1\n",
"7 6\n"
] | [
"Valera\n",
"Vladik\n"
] | Illustration for first test case:
<img class="tex-graphics" src="https://espresso.codeforces.com/ad9b7d0e481208de8e3a585aa1d96b9e1dda4fd7.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Illustration for second test case:
<img class="tex-graphics" src="https://espresso.codeforces.com/9f4836d2ccdffaee5a63898e5d4e6caf2ed4678c.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "1 1",
"output": "Valera"
},
{
"input": "7 6",
"output": "Vladik"
},
{
"input": "25 38",
"output": "Vladik"
},
{
"input": "8311 2468",
"output": "Valera"
},
{
"input": "250708 857756",
"output": "Vladik"
},
{
"input": "957985574 24997558",
"output": "Valera"
},
{
"input": "999963734 999994456",
"output": "Vladik"
},
{
"input": "1000000000 1000000000",
"output": "Vladik"
},
{
"input": "946 879",
"output": "Valera"
},
{
"input": "10819 45238",
"output": "Vladik"
},
{
"input": "101357 236928",
"output": "Vladik"
},
{
"input": "1033090 7376359",
"output": "Vladik"
},
{
"input": "9754309 9525494",
"output": "Valera"
},
{
"input": "90706344 99960537",
"output": "Vladik"
},
{
"input": "965161805 908862070",
"output": "Valera"
},
{
"input": "9 11",
"output": "Valera"
},
{
"input": "3 2",
"output": "Vladik"
},
{
"input": "6 6",
"output": "Vladik"
},
{
"input": "4 4",
"output": "Valera"
},
{
"input": "5 5",
"output": "Valera"
},
{
"input": "5 4",
"output": "Valera"
},
{
"input": "12345680 1",
"output": "Valera"
},
{
"input": "9 10",
"output": "Valera"
},
{
"input": "678 76687",
"output": "Vladik"
},
{
"input": "1 678",
"output": "Vladik"
},
{
"input": "45 1678",
"output": "Vladik"
},
{
"input": "3 3",
"output": "Vladik"
},
{
"input": "10 11",
"output": "Valera"
},
{
"input": "2 1",
"output": "Valera"
},
{
"input": "1 2",
"output": "Vladik"
},
{
"input": "2 2",
"output": "Vladik"
},
{
"input": "4 5",
"output": "Valera"
},
{
"input": "9 6",
"output": "Valera"
},
{
"input": "1 5",
"output": "Vladik"
},
{
"input": "7 8",
"output": "Vladik"
},
{
"input": "1000000000 999982505",
"output": "Valera"
},
{
"input": "12 12",
"output": "Vladik"
},
{
"input": "1000 950",
"output": "Valera"
},
{
"input": "10 9",
"output": "Valera"
},
{
"input": "100 9",
"output": "Valera"
},
{
"input": "1000 996",
"output": "Vladik"
},
{
"input": "9 5",
"output": "Valera"
}
] | 77 | 5,632,000 | 3 | 1,761 |
|
743 | Chloe and pleasant prizes | [
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took *n* prizes for the contestants and wrote on each of them a unique id (integer from 1 to *n*). A gift *i* is characterized by integer *a**i*Β β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with *n* vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105)Β β the number of gifts.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109)Β β the pleasantness of the gifts.
The next (*n*<=-<=1) lines contain two numbers each. The *i*-th of these lines contains integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the description of the tree's edges. It means that gifts with numbers *u**i* and *v**i* are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: *v**i* hangs on *u**i* or *u**i* hangs on *v**i*.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. | If it is possible for Chloe and Vladik to choose prizes without fighting, print single integerΒ β the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible. | [
"8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8\n",
"4\n1 -5 1 1\n1 2\n1 4\n2 3\n",
"1\n-1\n"
] | [
"25",
"2",
"Impossible"
] | none | [
{
"input": "8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8",
"output": "25"
},
{
"input": "4\n1 -5 1 1\n1 2\n1 4\n2 3",
"output": "2"
},
{
"input": "1\n-1",
"output": "Impossible"
},
{
"input": "10\n3 2 -4 2 0 3 3 3 3 4\n10 8\n4 2\n4 9\n3 5\n5 2\n7 4\n2 6\n1 8\n10 9",
"output": "6"
},
{
"input": "10\n-1 2 -2 -3 -1 -1 0 -4 -5 -4\n4 6\n6 9\n1 2\n6 2\n7 8\n7 9\n5 10\n6 3\n10 1",
"output": "-3"
},
{
"input": "10\n4 -4 2 5 -1 3 -1 1 4 5\n1 8\n7 1\n4 1\n9 6\n1 2\n5 10\n10 1\n9 3\n1 9",
"output": "14"
},
{
"input": "10\n29 -2 39 1 98 98 82 56 5 -2\n3 1\n7 9\n8 9\n7 3\n4 2\n5 10\n6 8\n10 6\n5 4",
"output": "Impossible"
},
{
"input": "10\n-3 0 1 -3 2 1 2 5 3 1\n6 2\n10 3\n10 5\n2 7\n3 4\n8 2\n8 10\n4 9\n1 9",
"output": "10"
},
{
"input": "10\n-5 0 1 -2 2 1 2 1 -1 -3\n10 4\n10 5\n4 1\n3 5\n2 8\n6 7\n9 7\n8 7\n6 3",
"output": "0"
},
{
"input": "10\n2 -4 5 4 -1 -5 -1 -5 3 -1\n8 6\n8 7\n10 2\n6 3\n5 2\n3 9\n10 1\n5 4\n9 4",
"output": "Impossible"
},
{
"input": "10\n-1 2 -5 -5 3 1 -4 0 3 -5\n2 10\n2 6\n8 4\n9 2\n10 7\n1 7\n9 5\n8 3\n9 4",
"output": "4"
},
{
"input": "10\n1 -4 -4 0 1 -3 1 -2 -4 2\n7 1\n7 6\n5 6\n4 2\n2 6\n3 9\n5 9\n10 6\n8 5",
"output": "2"
},
{
"input": "10\n-4 -1 -5 -1 -3 -1 -2 -2 -5 -3\n7 9\n3 6\n1 4\n7 2\n3 10\n8 9\n4 10\n3 5\n6 8",
"output": "-4"
},
{
"input": "10\n-21 -17 -16 -45 -93 -77 -73 -12 -81 -33\n9 5\n8 6\n1 10\n9 4\n3 2\n10 4\n3 8\n7 2\n5 6",
"output": "Impossible"
},
{
"input": "10\n-5 -2 -4 -1 -4 -5 -1 -4 -1 -3\n8 6\n7 2\n1 2\n10 4\n9 3\n6 10\n7 9\n5 4\n5 3",
"output": "Impossible"
},
{
"input": "10\n-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000\n7 8\n5 10\n8 6\n1 5\n7 9\n3 9\n2 10\n2 6\n4 3",
"output": "Impossible"
},
{
"input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n7 8\n8 3\n3 10\n2 10\n2 4\n9 6\n7 5\n6 5\n9 1",
"output": "Impossible"
},
{
"input": "3\n1 -1000000000 -1000000000\n1 2\n1 3",
"output": "-2000000000"
}
] | 1,855 | 228,966,400 | 3 | 1,765 |
|
813 | The Contest | [
"implementation"
] | null | null | Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!
This contest consists of *n* problems, and Pasha solves *i*th problem in *a**i* time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.
Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during *m* time periods, *j*th period is represented by its starting moment *l**j* and ending moment *r**j*. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment *T* iff there exists a period *x* such that *l**x*<=β€<=*T*<=β€<=*r**x*.
Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems. | The first line contains one integer *n*Β (1<=β€<=*n*<=β€<=1000) β the number of problems. The second line contains *n* integers *a**i*Β (1<=β€<=*a**i*<=β€<=105) β the time Pasha needs to solve *i*th problem.
The third line contains one integer *m*Β (0<=β€<=*m*<=β€<=1000) β the number of periods of time when the website is working. Next *m* lines represent these periods. *j*th line contains two numbers *l**j* and *r**j*Β (1<=β€<=*l**j*<=<<=*r**j*<=β€<=105) β the starting and the ending moment of *j*th period.
It is guaranteed that the periods are not intersecting and are given in chronological order, so for every *j*<=><=1 the condition *l**j*<=><=*r**j*<=-<=1 is met. | If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.
Otherwise print "-1" (without brackets). | [
"2\n3 4\n2\n1 4\n7 9\n",
"1\n5\n1\n1 4\n",
"1\n5\n1\n1 5\n"
] | [
"7\n",
"-1\n",
"5\n"
] | In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.
In the second example Pasha invents the solution only after the website stops working for the last time.
In the third example Pasha sends the solution exactly at the end of the first period. | [
{
"input": "2\n3 4\n2\n1 4\n7 9",
"output": "7"
},
{
"input": "1\n5\n1\n1 4",
"output": "-1"
},
{
"input": "1\n5\n1\n1 5",
"output": "5"
},
{
"input": "5\n100000 100000 100000 100000 100000\n0",
"output": "-1"
},
{
"input": "5\n886 524 128 4068 298\n3\n416 3755\n4496 11945\n17198 18039",
"output": "5904"
},
{
"input": "10\n575 3526 1144 1161 889 1038 790 19 765 357\n2\n4475 10787\n16364 21678",
"output": "10264"
},
{
"input": "1\n4\n1\n5 9",
"output": "5"
},
{
"input": "1\n200\n4\n1 10\n20 40\n50 55\n190 210",
"output": "200"
},
{
"input": "4\n643 70 173 745\n14\n990 995\n1256 1259\n1494 1499\n1797 1804\n2443 2450\n2854 2859\n3164 3167\n4084 4092\n4615 4622\n5555 5563\n6412 6421\n7173 7180\n7566 7571\n8407 8415",
"output": "1797"
},
{
"input": "42\n749 516 256 497 37 315 184 518 103 726 80 983 474 884 209 706 10 543 587 371 199 315 967 707 948 736 590 734 715 184 230 513 199 898 287 468 250 600 352 29 408 22\n2\n312 314\n1293 1302",
"output": "-1"
},
{
"input": "1\n10000\n2\n1 10\n9998 10000",
"output": "10000"
},
{
"input": "1\n547\n15\n774 779\n1598 1605\n2458 2464\n3138 3140\n3372 3378\n4268 4272\n4730 4733\n5064 5067\n5074 5075\n5483 5490\n5894 5901\n5931 5938\n6750 6756\n7487 7491\n8328 8332",
"output": "774"
},
{
"input": "1\n10\n2\n1 2\n11 12",
"output": "11"
},
{
"input": "2\n4 6\n2\n5 10\n15 20",
"output": "10"
},
{
"input": "2\n16 5\n3\n5 10\n15 20\n25 30",
"output": "25"
},
{
"input": "1\n16\n2\n5 10\n15 20",
"output": "16"
},
{
"input": "44\n750 672 846 969 981 698 380 968 813 587 156 28 446 917 849 449 173 764 226 958 335 622 236 782 416 689 113 728 452 265 585 217 707 50 520 712 946 275 423 123 175 268 583 528\n4\n869 870\n1353 1354\n1683 1685\n2532 2540",
"output": "-1"
},
{
"input": "1\n1\n0",
"output": "-1"
},
{
"input": "3\n1 2 5\n3\n5 6\n7 8\n9 13",
"output": "8"
},
{
"input": "1\n2\n0",
"output": "-1"
},
{
"input": "1\n5\n3\n1 2\n3 4\n10 11",
"output": "10"
},
{
"input": "1\n4\n0",
"output": "-1"
},
{
"input": "1\n5\n0",
"output": "-1"
},
{
"input": "1\n239\n0",
"output": "-1"
}
] | 31 | 0 | 3 | 1,768 |
|
380 | Sereja and Cinema | [
"combinatorics",
"math"
] | null | null | The cinema theater hall in Sereja's city is *n* seats lined up in front of one large screen. There are slots for personal possessions to the left and to the right of each seat. Any two adjacent seats have exactly one shared slot. The figure below shows the arrangement of seats and slots for *n*<==<=4.
Today it's the premiere of a movie called "Dry Hard". The tickets for all the seats have been sold. There is a very strict controller at the entrance to the theater, so all *n* people will come into the hall one by one. As soon as a person enters a cinema hall, he immediately (momentarily) takes his seat and occupies all empty slots to the left and to the right from him. If there are no empty slots, the man gets really upset and leaves.
People are not very constant, so it's hard to predict the order in which the viewers will enter the hall. For some seats, Sereja knows the number of the viewer (his number in the entering queue of the viewers) that will come and take this seat. For others, it can be any order.
Being a programmer and a mathematician, Sereja wonders: how many ways are there for the people to enter the hall, such that nobody gets upset? As the number can be quite large, print it modulo 1000000007 (109<=+<=7). | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers, the *i*-th integer shows either the index of the person (index in the entering queue) with the ticket for the *i*-th seat or a 0, if his index is not known. It is guaranteed that all positive numbers in the second line are distinct.
You can assume that the index of the person who enters the cinema hall is a unique integer from 1 to *n*. The person who has index 1 comes first to the hall, the person who has index 2 comes second and so on. | In a single line print the remainder after dividing the answer by number 1000000007 (109<=+<=7). | [
"11\n0 0 0 0 0 0 0 0 0 0 0\n",
"6\n0 3 1 0 0 0\n"
] | [
"1024\n",
"3\n"
] | none | [] | 1,000 | 16,486,400 | 0 | 1,772 |
|
950 | Intercepted Message | [
"greedy",
"implementation"
] | null | null | Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.
Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred through the network: if an archive consists of *k* files of sizes *l*1,<=*l*2,<=...,<=*l**k* bytes, then the *i*-th file is split to one or more blocks *b**i*,<=1,<=*b**i*,<=2,<=...,<=*b**i*,<=*m**i* (here the total length of the blocks *b**i*,<=1<=+<=*b**i*,<=2<=+<=...<=+<=*b**i*,<=*m**i* is equal to the length of the file *l**i*), and after that all blocks are transferred through the network, maintaining the order of files in the archive.
Zhorik thinks that the two messages contain the same archive, because their total lengths are equal. However, each file can be split in blocks in different ways in the two messages.
You are given the lengths of blocks in each of the two messages. Help Zhorik to determine what is the maximum number of files could be in the archive, if the Zhorik's assumption is correct. | The first line contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of blocks in the first and in the second messages.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=β€<=*x**i*<=β€<=106) β the length of the blocks that form the first message.
The third line contains *m* integers *y*1,<=*y*2,<=...,<=*y**m* (1<=β€<=*y**i*<=β€<=106) β the length of the blocks that form the second message.
It is guaranteed that *x*1<=+<=...<=+<=*x**n*<==<=*y*1<=+<=...<=+<=*y**m*. Also, it is guaranteed that *x*1<=+<=...<=+<=*x**n*<=β€<=106. | Print the maximum number of files the intercepted array could consist of. | [
"7 6\n2 5 3 1 11 4 4\n7 8 2 4 1 8\n",
"3 3\n1 10 100\n1 100 10\n",
"1 4\n4\n1 1 1 1\n"
] | [
"3\n",
"2\n",
"1\n"
] | In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2β+β5β=β7, 15β=β3β+β1β+β11β=β8β+β2β+β4β+β1 and 4β+β4β=β8.
In the second example it is possible that the archive contains two files of sizes 1 and 110β=β10β+β100β=β100β+β10. Note that the order of files is kept while transferring archives through the network, so we can't say that there are three files of sizes 1, 10 and 100.
In the third example the only possibility is that the archive contains a single file of size 4. | [
{
"input": "7 6\n2 5 3 1 11 4 4\n7 8 2 4 1 8",
"output": "3"
},
{
"input": "3 3\n1 10 100\n1 100 10",
"output": "2"
},
{
"input": "1 4\n4\n1 1 1 1",
"output": "1"
},
{
"input": "1 1\n1000000\n1000000",
"output": "1"
},
{
"input": "3 5\n2 2 9\n2 1 4 2 4",
"output": "2"
},
{
"input": "5 3\n1 1 4 1 2\n1 4 4",
"output": "2"
},
{
"input": "30 50\n3 3 1 3 1 2 4 3 4 1 3 2 3 3 2 3 2 1 3 4 2 1 1 3 2 2 1 3 1 60\n4 4 1 2 2 2 3 1 3 2 1 2 4 4 2 1 2 3 1 3 4 4 3 3 4 4 4 1 2 1 3 3 1 1 3 3 4 3 2 3 2 4 1 4 2 3 2 2 3 1",
"output": "12"
},
{
"input": "50 50\n5733 740 547 3647 5382 5109 6842 7102 5879 1502 3574 1628 7905 4357 8569 9564 8268 3542 2487 8532 425 7713 2585 925 6458 2697 2844 69 324 9030 495 4428 6724 3524 3304 4874 1303 2098 1136 1048 2464 7316 274 9586 534 2450 2368 8060 7795 70692\n1918 4122 6806 4914 6517 6278 9842 9480 6609 4221 9373 1728 9508 9778 8578 5589 2673 6618 6031 9016 4017 6671 6008 2268 5154 9614 6834 9512 9618 6424 1736 1464 6520 9812 1722 9197 2412 2699 73 968 2906 2715 6573 8675 548 7061 5455 88 5565 2544",
"output": "1"
},
{
"input": "1 2\n2\n1 1",
"output": "1"
},
{
"input": "1 2\n1000000\n999999 1",
"output": "1"
},
{
"input": "2 2\n1 1\n1 1",
"output": "2"
},
{
"input": "2 2\n500000 500000\n1 999999",
"output": "1"
},
{
"input": "2 2\n2 3\n4 1",
"output": "1"
},
{
"input": "2 2\n2 3\n3 2",
"output": "1"
},
{
"input": "2 2\n2 3\n2 3",
"output": "2"
},
{
"input": "2 3\n2 2\n1 1 2",
"output": "2"
},
{
"input": "1 1\n1\n1",
"output": "1"
},
{
"input": "2 3\n3 2\n2 1 2",
"output": "2"
},
{
"input": "2 3\n2 3\n2 1 2",
"output": "2"
},
{
"input": "50 30\n2 3 1 2 2 4 3 4 3 2 1 4 2 3 1 3 1 2 2 3 1 1 1 2 3 1 4 3 1 2 1 2 2 1 2 4 4 3 3 2 2 1 1 1 2 2 2 4 3 3\n3 3 3 4 1 4 1 4 4 1 3 4 3 1 2 4 2 1 4 2 3 1 1 2 2 1 2 4 1 41",
"output": "12"
},
{
"input": "50 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\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",
"output": "50"
},
{
"input": "31 31\n5745 258 5486 13779 20931 407 1478 49032 30787 4957 36603 1034 5011 22319 50560 34419 22036 18235 62551 89259 36093 126169 106027 1673 52983 50127 640 30714 54574 20129 45984\n5745 258 5486 13779 20931 407 1478 49032 30787 4957 36603 1034 5011 22319 50560 34419 22036 18235 62551 89259 36093 126169 106027 1673 52983 50127 640 30714 54574 20129 45984",
"output": "31"
},
{
"input": "3 6\n8 4 1\n1 8 1 1 1 1",
"output": "2"
}
] | 202 | 30,720,000 | 3 | 1,775 |
|
984 | Game | [
"sortings"
] | null | null | Two players play a game.
Initially there are $n$ integers $a_1, a_2, \ldots, a_n$ written on the board. Each turn a player selects one number and erases it from the board. This continues until there is only one number left on the board, i.Β e. $n - 1$ turns are made. The first player makes the first move, then players alternate turns.
The first player wants to minimize the last number that would be left on the board, while the second player wants to maximize it.
You want to know what number will be left on the board after $n - 1$ turns if both players make optimal moves. | The first line contains one integer $n$ ($1 \le n \le 1000$)Β β the number of numbers on the board.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^6$). | Print one number that will be left on the board. | [
"3\n2 1 3\n",
"3\n2 2 2\n"
] | [
"2",
"2"
] | In the first sample, the first player erases $3$ and the second erases $1$. $2$ is left on the board.
In the second sample, $2$ is left on the board regardless of the actions of the players. | [
{
"input": "3\n2 1 3",
"output": "2"
},
{
"input": "3\n2 2 2",
"output": "2"
},
{
"input": "9\n44 53 51 80 5 27 74 79 94",
"output": "53"
},
{
"input": "10\n38 82 23 37 96 4 81 60 67 86",
"output": "60"
},
{
"input": "10\n58 26 77 15 53 81 68 48 22 65",
"output": "53"
},
{
"input": "1\n124",
"output": "124"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "3\n1 1 1000",
"output": "1"
},
{
"input": "2\n322 322",
"output": "322"
},
{
"input": "3\n9 92 12",
"output": "12"
},
{
"input": "3\n1 2 2",
"output": "2"
}
] | 46 | 0 | 0 | 1,777 |
|
459 | Pashmak and Garden | [
"implementation"
] | null | null | Pashmak has fallen in love with an attractive girl called Parmida since one year ago...
Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones. | The first line contains four space-separated *x*1,<=*y*1,<=*x*2,<=*y*2 (<=-<=100<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=100) integers, where *x*1 and *y*1 are coordinates of the first tree and *x*2 and *y*2 are coordinates of the second tree. It's guaranteed that the given points are distinct. | If there is no solution to the problem, print -1. Otherwise print four space-separated integers *x*3,<=*y*3,<=*x*4,<=*y*4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them.
Note that *x*3,<=*y*3,<=*x*4,<=*y*4 must be in the range (<=-<=1000<=β€<=*x*3,<=*y*3,<=*x*4,<=*y*4<=β€<=1000). | [
"0 0 0 1\n",
"0 0 1 1\n",
"0 0 1 2\n"
] | [
"1 0 1 1\n",
"0 1 1 0\n",
"-1\n"
] | none | [
{
"input": "0 0 0 1",
"output": "1 0 1 1"
},
{
"input": "0 0 1 1",
"output": "0 1 1 0"
},
{
"input": "0 0 1 2",
"output": "-1"
},
{
"input": "-100 -100 100 100",
"output": "-100 100 100 -100"
},
{
"input": "-100 -100 99 100",
"output": "-1"
},
{
"input": "0 -100 0 100",
"output": "200 -100 200 100"
},
{
"input": "27 -74 27 74",
"output": "175 -74 175 74"
},
{
"input": "0 1 2 3",
"output": "0 3 2 1"
},
{
"input": "-100 100 100 -100",
"output": "-100 -100 100 100"
},
{
"input": "-100 -100 -100 100",
"output": "100 -100 100 100"
},
{
"input": "100 100 100 -100",
"output": "300 100 300 -100"
},
{
"input": "100 -100 -100 -100",
"output": "100 100 -100 100"
},
{
"input": "-100 100 100 100",
"output": "-100 300 100 300"
},
{
"input": "0 1 0 0",
"output": "1 1 1 0"
},
{
"input": "1 1 0 0",
"output": "1 0 0 1"
},
{
"input": "0 0 1 0",
"output": "0 1 1 1"
},
{
"input": "1 0 0 1",
"output": "1 1 0 0"
},
{
"input": "1 0 1 1",
"output": "2 0 2 1"
},
{
"input": "1 1 0 1",
"output": "1 2 0 2"
},
{
"input": "15 -9 80 -9",
"output": "15 56 80 56"
},
{
"input": "51 -36 18 83",
"output": "-1"
},
{
"input": "69 -22 60 16",
"output": "-1"
},
{
"input": "-68 -78 -45 -55",
"output": "-68 -55 -45 -78"
},
{
"input": "68 -92 8 -32",
"output": "68 -32 8 -92"
},
{
"input": "95 -83 -39 -6",
"output": "-1"
},
{
"input": "54 94 53 -65",
"output": "-1"
},
{
"input": "-92 15 84 15",
"output": "-92 191 84 191"
},
{
"input": "67 77 -11 -1",
"output": "67 -1 -11 77"
},
{
"input": "91 -40 30 21",
"output": "91 21 30 -40"
},
{
"input": "66 -64 -25 -64",
"output": "66 27 -25 27"
},
{
"input": "-42 84 -67 59",
"output": "-42 59 -67 84"
},
{
"input": "73 47 -5 -77",
"output": "-1"
},
{
"input": "6 85 -54 -84",
"output": "-1"
},
{
"input": "-58 -55 40 43",
"output": "-58 43 40 -55"
},
{
"input": "56 22 48 70",
"output": "-1"
},
{
"input": "-17 -32 76 -32",
"output": "-17 61 76 61"
},
{
"input": "0 2 2 0",
"output": "0 0 2 2"
},
{
"input": "0 0 -1 1",
"output": "0 1 -1 0"
},
{
"input": "0 2 1 1",
"output": "0 1 1 2"
},
{
"input": "0 0 1 -1",
"output": "0 -1 1 0"
},
{
"input": "-1 2 -2 3",
"output": "-1 3 -2 2"
},
{
"input": "0 1 1 0",
"output": "0 0 1 1"
},
{
"input": "1 2 2 1",
"output": "1 1 2 2"
},
{
"input": "4 1 2 1",
"output": "4 3 2 3"
},
{
"input": "70 0 0 10",
"output": "-1"
},
{
"input": "2 3 4 1",
"output": "2 1 4 3"
},
{
"input": "1 3 3 1",
"output": "1 1 3 3"
},
{
"input": "-3 3 0 0",
"output": "-3 0 0 3"
},
{
"input": "2 8 7 3",
"output": "2 3 7 8"
},
{
"input": "1 2 2 3",
"output": "1 3 2 2"
},
{
"input": "0 3 3 0",
"output": "0 0 3 3"
},
{
"input": "0 0 -3 3",
"output": "0 3 -3 0"
},
{
"input": "0 2 1 2",
"output": "0 3 1 3"
},
{
"input": "1 1 2 0",
"output": "1 0 2 1"
},
{
"input": "0 0 5 0",
"output": "0 5 5 5"
},
{
"input": "3 4 7 8",
"output": "3 8 7 4"
},
{
"input": "0 5 5 0",
"output": "0 0 5 5"
},
{
"input": "5 6 8 3",
"output": "5 3 8 6"
},
{
"input": "2 2 1 1",
"output": "2 1 1 2"
},
{
"input": "0 1 3 1",
"output": "0 4 3 4"
},
{
"input": "2 4 5 4",
"output": "2 7 5 7"
},
{
"input": "0 5 1 5",
"output": "0 6 1 6"
},
{
"input": "4 0 0 4",
"output": "4 4 0 0"
},
{
"input": "0 1 1 8",
"output": "-1"
},
{
"input": "2 3 3 4",
"output": "2 4 3 3"
},
{
"input": "1 0 2 1",
"output": "1 1 2 0"
},
{
"input": "0 0 2 14",
"output": "-1"
},
{
"input": "0 0 4 3",
"output": "-1"
},
{
"input": "3 5 5 3",
"output": "3 3 5 5"
},
{
"input": "-1 1 1 -1",
"output": "-1 -1 1 1"
},
{
"input": "0 0 2 0",
"output": "0 2 2 2"
},
{
"input": "0 0 1 7",
"output": "-1"
},
{
"input": "1 2 3 2",
"output": "1 4 3 4"
},
{
"input": "1 12 3 10",
"output": "1 10 3 12"
}
] | 46 | 0 | 0 | 1,785 |
|
932 | Palindromic Supersequence | [
"constructive algorithms"
] | null | null | You are given a string *A*. Find a string *B*, where *B* is a palindrome and *A* is a subsequence of *B*.
A subsequence of a string is a string that can be derived from it by deleting some (not necessarily consecutive) characters without changing the order of the remaining characters. For example, "cotst" is a subsequence of "contest".
A palindrome is a string that reads the same forward or backward.
The length of string *B* should be at most 104. It is guaranteed that there always exists such string.
You do not need to find the shortest answer, the only restriction is that the length of string *B* should not exceed 104. | First line contains a string *A* (1<=β€<=|*A*|<=β€<=103) consisting of lowercase Latin letters, where |*A*| is a length of *A*. | Output single line containing *B* consisting of only lowercase Latin letters. You do not need to find the shortest answer, the only restriction is that the length of string *B* should not exceed 104. If there are many possible *B*, print any of them. | [
"aba\n",
"ab\n"
] | [
"aba",
"aabaa"
] | In the first example, "aba" is a subsequence of "aba" which is a palindrome.
In the second example, "ab" is a subsequence of "aabaa" which is a palindrome. | [
{
"input": "aba",
"output": "abaaba"
},
{
"input": "ab",
"output": "abba"
},
{
"input": "krnyoixirslfszfqivgkaflgkctvbvksipwomqxlyqxhlbceuhbjbfnhofcgpgwdseffycthmlpcqejgskwjkbkbbmifnurnwyhevsoqzmtvzgfiqajfrgyuzxnrtxectcnlyoisbglpdbjbslxlpoymrcxmdtqhcnlvtqdwftuzgbdxsyscwbrguostbelnvtaqdmkmihmoxqtqlxvlsssisvqvvzotoyqryuyqwoknnqcqggysrqpkrccvyhxsjmhoqoyocwcriplarjoyiqrmmpmueqbsbljddwrumauczfziodpudheexalbwpiypmdjlmwtgdrzhpxneofhqzjdmurgvmrwdotuwyknlrbvuvtnhiouvqitgyfgfieonbaapyhwpcrmehxcpkijzfiayfvoxkpa",
"output": "krnyoixirslfszfqivgkaflgkctvbvksipwomqxlyqxhlbceuhbjbfnhofcgpgwdseffycthmlpcqejgskwjkbkbbmifnurnwyhevsoqzmtvzgfiqajfrgyuzxnrtxectcnlyoisbglpdbjbslxlpoymrcxmdtqhcnlvtqdwftuzgbdxsyscwbrguostbelnvtaqdmkmihmoxqtqlxvlsssisvqvvzotoyqryuyqwoknnqcqggysrqpkrccvyhxsjmhoqoyocwcriplarjoyiqrmmpmueqbsbljddwrumauczfziodpudheexalbwpiypmdjlmwtgdrzhpxneofhqzjdmurgvmrwdotuwyknlrbvuvtnhiouvqitgyfgfieonbaapyhwpcrmehxcpkijzfiayfvoxkpaapkxovfyaifzjikpcxhemrcpwhypaabnoeifgfygtiqvuoihntvuvbrlnkywutodwrmvgrumdjzqhfoenxphzrdgtwmljdm..."
},
{
"input": "mgrfmzxqpejcixxppqgvuawutgrmezjkteofjbnrvzzkvjtacfxjjokisavsgrslryxfqgrmdsqwptajbqzvethuljbdatxghfzqrwvfgakwmoawlzqjypmhllbbuuhbpriqsnibywlgjlxowyzagrfnqafvcqwktkcjwejevzbnxhsfmwojshcdypnvbuhhuzqmgovmvgwiizatoxgblyudipahfbkewmuneoqhjmbpdtwnznblwvtjrniwlbyblhppndspojrouffazpoxtqdfpjuhitvijrohavpqatofxwmksvjcvhdecxwwmosqiczjpkfafqlboxosnjgzgdraehzdltthemeusxhiiimrdrugabnxwsygsktkcslhjebfexucsyvlwrptebkjhefsvfrmcqqdlanbetrgzwylizmrystvpgrkhlicfadco",
"output": "mgrfmzxqpejcixxppqgvuawutgrmezjkteofjbnrvzzkvjtacfxjjokisavsgrslryxfqgrmdsqwptajbqzvethuljbdatxghfzqrwvfgakwmoawlzqjypmhllbbuuhbpriqsnibywlgjlxowyzagrfnqafvcqwktkcjwejevzbnxhsfmwojshcdypnvbuhhuzqmgovmvgwiizatoxgblyudipahfbkewmuneoqhjmbpdtwnznblwvtjrniwlbyblhppndspojrouffazpoxtqdfpjuhitvijrohavpqatofxwmksvjcvhdecxwwmosqiczjpkfafqlboxosnjgzgdraehzdltthemeusxhiiimrdrugabnxwsygsktkcslhjebfexucsyvlwrptebkjhefsvfrmcqqdlanbetrgzwylizmrystvpgrkhlicfadcoocdafcilhkrgpvtsyrmzilywzgrtebnaldqqcmrfvsfehjkbetprwlvyscuxef..."
},
{
"input": "hdmasfcjuigrwjchmjslmpynewnzpphmudzcbxzdexjuhktdtcoibzvevsmwaxakrtdfoivkvoooypyemiidadquqepxwqkesdnakxkbzrcjkgvwwxtqxvfpxcwitljyehldgsjytmekimkkndjvnzqtjykiymkmdzpwakxdtkzcqcatlevppgfhyykgmipuodjrnfjzhcmjdbzvhywprbwdcfxiffpzbjbmbyijkqnosslqbfvvicxvoeuzruraetglthgourzhfpnubzvblfzmmbgepjjyshchthulxar",
"output": "hdmasfcjuigrwjchmjslmpynewnzpphmudzcbxzdexjuhktdtcoibzvevsmwaxakrtdfoivkvoooypyemiidadquqepxwqkesdnakxkbzrcjkgvwwxtqxvfpxcwitljyehldgsjytmekimkkndjvnzqtjykiymkmdzpwakxdtkzcqcatlevppgfhyykgmipuodjrnfjzhcmjdbzvhywprbwdcfxiffpzbjbmbyijkqnosslqbfvvicxvoeuzruraetglthgourzhfpnubzvblfzmmbgepjjyshchthulxarraxluhthchsyjjpegbmmzflbvzbunpfhzruoghtlgtearurzueovxcivvfbqlssonqkjiybmbjbzpffixfcdwbrpwyhvzbdjmchzjfnrjdoupimgkyyhfgppveltacqczktdxkawpzdmkmyikyjtqznvjdnkkmikemtyjsgdlheyjltiwcxpfvxqtxwwvgkjcrzbkxkandsekqwxpequ..."
},
{
"input": "fggbyzobbmxtwdajawqdywnppflkkmtxzjvxopqvliwdwhzepcuiwelhbuotlkvesexnwkytonfrpqcxzzqzdvsmbsjcxxeugavekozfjlolrtqgwzqxsfgrnvrgfrqpixhsskbpzghndesvwptpvvkasfalzsetopervpwzmkgpcexqnvtnoulprwnowmsorscecvvvrjfwumcjqyrounqsgdruxttvtmrkivtxauhosokdiahsyrftzsgvgyveqwkzhqstbgywrvmsgfcfyuxpphvmyydzpohgdicoxbtjnsbyhoidnkrialowvlvmjpxcfeygqzphmbcjkupojsmmuqlydixbaluwezvnfasjfxilbyllwyipsmovdzosuwotcxerzcfuvxprtziseshjfcosalyqglpotxvxaanpocypsiyazsejjoximnbvqucftuvdksaxutvjeunodbipsumlaymjnzljurefjg",
"output": "fggbyzobbmxtwdajawqdywnppflkkmtxzjvxopqvliwdwhzepcuiwelhbuotlkvesexnwkytonfrpqcxzzqzdvsmbsjcxxeugavekozfjlolrtqgwzqxsfgrnvrgfrqpixhsskbpzghndesvwptpvvkasfalzsetopervpwzmkgpcexqnvtnoulprwnowmsorscecvvvrjfwumcjqyrounqsgdruxttvtmrkivtxauhosokdiahsyrftzsgvgyveqwkzhqstbgywrvmsgfcfyuxpphvmyydzpohgdicoxbtjnsbyhoidnkrialowvlvmjpxcfeygqzphmbcjkupojsmmuqlydixbaluwezvnfasjfxilbyllwyipsmovdzosuwotcxerzcfuvxprtziseshjfcosalyqglpotxvxaanpocypsiyazsejjoximnbvqucftuvdksaxutvjeunodbipsumlaymjnzljurefjggjferujlznjmyalmuspib..."
},
{
"input": "qyyxqkbxsvfnjzttdqmpzinbdgayllxpfrpopwciejjjzadguurnnhvixgueukugkkjyghxknedojvmdrskswiotgatsajowionuiumuhyggjuoympuxyfahwftwufvocdguxmxabbxnfviscxtilzzauizsgugwcqtbqgoosefhkumhodwpgolfdkbuiwlzjydonwbgyzzrjwxnceltqgqelrrljmzdbftmaogiuosaqhngmdzxzlmyrwefzhqawmkdckfnyyjgdjgadtfjvrkdwysqofcgyqrnyzutycvspzbjmmesobvhshtqlrytztyieknnkporrbcmlopgtknlmsstzkigreqwgsvagmvbrvwypoxttmzzsgm",
"output": "qyyxqkbxsvfnjzttdqmpzinbdgayllxpfrpopwciejjjzadguurnnhvixgueukugkkjyghxknedojvmdrskswiotgatsajowionuiumuhyggjuoympuxyfahwftwufvocdguxmxabbxnfviscxtilzzauizsgugwcqtbqgoosefhkumhodwpgolfdkbuiwlzjydonwbgyzzrjwxnceltqgqelrrljmzdbftmaogiuosaqhngmdzxzlmyrwefzhqawmkdckfnyyjgdjgadtfjvrkdwysqofcgyqrnyzutycvspzbjmmesobvhshtqlrytztyieknnkporrbcmlopgtknlmsstzkigreqwgsvagmvbrvwypoxttmzzsgmmgszzmttxopywvrbvmgavsgwqergikztssmlnktgpolmcbrropknnkeiytztyrlqthshvbosemmjbzpsvcytuzynrqygcfoqsywdkrvjftdagjdgjyynfkcdkmwaqhzfewry..."
},
{
"input": "scvlhflaqvniyiyofonowwcuqajuwscdrzhbvasymvqfnthzvtjcfuaftrbjghhvslcohwpxkggrbtatjtgehuqtorwinwvrtdldyoeeozxwippuahgkuehvsmyqtodqvlufqqmqautaqirvwzvtodzxtgxiinubhrbeoiybidutrqamsdnasctxatzkvkjkrmavdravnsxyngjlugwftmhmcvvxdbfndurrbmcpuoigjpssqcortmqoqttrabhoqvopjkxvpbqdqsilvlplhgqazauyvnodsxtwnomlinjpozwhrgrkqwmlwcwdkxjxjftexiavwrejvdjcfptterblxysjcheesyqsbgdrzjxbfjqgjgmvccqcyj",
"output": "scvlhflaqvniyiyofonowwcuqajuwscdrzhbvasymvqfnthzvtjcfuaftrbjghhvslcohwpxkggrbtatjtgehuqtorwinwvrtdldyoeeozxwippuahgkuehvsmyqtodqvlufqqmqautaqirvwzvtodzxtgxiinubhrbeoiybidutrqamsdnasctxatzkvkjkrmavdravnsxyngjlugwftmhmcvvxdbfndurrbmcpuoigjpssqcortmqoqttrabhoqvopjkxvpbqdqsilvlplhgqazauyvnodsxtwnomlinjpozwhrgrkqwmlwcwdkxjxjftexiavwrejvdjcfptterblxysjcheesyqsbgdrzjxbfjqgjgmvccqcyjjycqccvmgjgqjfbxjzrdgbsqyseehcjsyxlbrettpfcjdvjerwvaixetfjxjxkdwcwlmwqkrgrhwzopjnilmonwtxsdonvyuazaqghlplvlisqdqbpvxkjpovqohbarttqoqm..."
},
{
"input": "oohkqxxtvxzmvfjjxyjwlbqmeqwwlienzkdbhswgfbkhfygltsucdijozwaiewpixapyazfztksjeoqjugjfhdbqzuezbuajfvvffkwprroyivfoocvslejffgxuiofisenroxoeixmdbzonmreikpflciwsbafrdqfvdfojgoziiibqhwwsvhnzmptgirqqulkgmyzrfekzqqujmdumxkudsgexisupedisgmdgebvlvrpyfrbrqjknrxyzfpwmsxjxismgd",
"output": "oohkqxxtvxzmvfjjxyjwlbqmeqwwlienzkdbhswgfbkhfygltsucdijozwaiewpixapyazfztksjeoqjugjfhdbqzuezbuajfvvffkwprroyivfoocvslejffgxuiofisenroxoeixmdbzonmreikpflciwsbafrdqfvdfojgoziiibqhwwsvhnzmptgirqqulkgmyzrfekzqqujmdumxkudsgexisupedisgmdgebvlvrpyfrbrqjknrxyzfpwmsxjxismgddgmsixjxsmwpfzyxrnkjqrbrfyprvlvbegdmgsidepusixegsdukxmudmjuqqzkefrzymgkluqqrigtpmznhvswwhqbiiizogjofdvfqdrfabswiclfpkiermnozbdmxieoxornesifoiuxgffjelsvcoofviyorrpwkffvvfjaubzeuzqbdhfjgujqoejsktzfzaypaxipweiawzojidcustlgyfhkbfgwshbdkzneilwwqemqblw..."
},
{
"input": "gilhoixzjgidfanqrmekjelnvicpuujlpxittgadgrhqallnkjlemwazntwfywjnrxdkgrnczlwzjyeyfktduzdjnivcldjjarfzmmdbyytvipbbnjqolfnlqjpidotxxfobgtgpvjmpddcyddwdcjsxxumuoyznhpvpqccgqnuouzojntanfwctthcgynrukcvshsuuqrxfdvqqggaatwytikkitywtaaggqqvdfxrquushsvckurnygchttcwfnatnjozuounqgccqpvphnzyoumuxxsjcdwddycddpmjvpgtgbofxxtodipjqlnfloqjnbbpivtyybdmmzfrajjdlcvinjdzudtkfyeyjzwlzcnrgkdxrnjwyfwtnzawmeljknllaqhrgdagttixpljuupcivnlejkemrqnafdigjzxiohlig",
"output": "gilhoixzjgidfanqrmekjelnvicpuujlpxittgadgrhqallnkjlemwazntwfywjnrxdkgrnczlwzjyeyfktduzdjnivcldjjarfzmmdbyytvipbbnjqolfnlqjpidotxxfobgtgpvjmpddcyddwdcjsxxumuoyznhpvpqccgqnuouzojntanfwctthcgynrukcvshsuuqrxfdvqqggaatwytikkitywtaaggqqvdfxrquushsvckurnygchttcwfnatnjozuounqgccqpvphnzyoumuxxsjcdwddycddpmjvpgtgbofxxtodipjqlnfloqjnbbpivtyybdmmzfrajjdlcvinjdzudtkfyeyjzwlzcnrgkdxrnjwyfwtnzawmeljknllaqhrgdagttixpljuupcivnlejkemrqnafdigjzxiohliggilhoixzjgidfanqrmekjelnvicpuujlpxittgadgrhqallnkjlemwazntwfywjnrxdkgrnczlw..."
},
{
"input": "abcab",
"output": "abcabbacba"
},
{
"input": "baaaaaaa",
"output": "baaaaaaaaaaaaaab"
},
{
"input": "baaaaaa",
"output": "baaaaaaaaaaaab"
},
{
"input": "baaaaaaaaa",
"output": "baaaaaaaaaaaaaaaaaab"
},
{
"input": "baaaaaaaa",
"output": "baaaaaaaaaaaaaaaab"
}
] | 46 | 5,632,000 | -1 | 1,791 |
|
165 | Burning Midnight Oil | [
"binary search",
"implementation"
] | null | null | One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of *n* lines of code. Vasya is already exhausted, so he works like that: first he writes *v* lines of code, drinks a cup of tea, then he writes as much as lines, drinks another cup of tea, then he writes lines and so on: , , , ...
The expression is regarded as the integral part from dividing number *a* by number *b*.
The moment the current value equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value *v* can take to let him write not less than *n* lines of code before he falls asleep. | The input consists of two integers *n* and *k*, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1<=β€<=*n*<=β€<=109, 2<=β€<=*k*<=β€<=10. | Print the only integer β the minimum value of *v* that lets Vasya write the program in one night. | [
"7 2\n",
"59 9\n"
] | [
"4\n",
"54\n"
] | In the first sample the answer is *v*β=β4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4β+β2β+β1β=β7 lines in a night and complete the task.
In the second sample the answer is *v*β=β54. Vasya writes the code in the following portions: 54, 6. The total sum is 54β+β6β=β60, that's even more than *n*β=β59. | [
{
"input": "7 2",
"output": "4"
},
{
"input": "59 9",
"output": "54"
},
{
"input": "1 9",
"output": "1"
},
{
"input": "11 2",
"output": "7"
},
{
"input": "747 2",
"output": "376"
},
{
"input": "6578 2",
"output": "3293"
},
{
"input": "37212 2",
"output": "18609"
},
{
"input": "12357 2",
"output": "6181"
},
{
"input": "7998332 2",
"output": "3999172"
},
{
"input": "86275251 2",
"output": "43137632"
},
{
"input": "75584551 2",
"output": "37792280"
},
{
"input": "6 3",
"output": "5"
},
{
"input": "43 4",
"output": "33"
},
{
"input": "811 3",
"output": "543"
},
{
"input": "3410 4",
"output": "2560"
},
{
"input": "21341 4",
"output": "16009"
},
{
"input": "696485 4",
"output": "522368"
},
{
"input": "8856748 3",
"output": "5904504"
},
{
"input": "2959379 4",
"output": "2219538"
},
{
"input": "831410263 3",
"output": "554273516"
},
{
"input": "2 5",
"output": "2"
},
{
"input": "19 6",
"output": "17"
},
{
"input": "715 7",
"output": "615"
},
{
"input": "9122 5",
"output": "7300"
},
{
"input": "89117 6",
"output": "74268"
},
{
"input": "689973 7",
"output": "591408"
},
{
"input": "3024524 5",
"output": "2419624"
},
{
"input": "67127156 6",
"output": "55939302"
},
{
"input": "412262167 7",
"output": "353367574"
},
{
"input": "6 8",
"output": "6"
},
{
"input": "59 9",
"output": "54"
},
{
"input": "246 10",
"output": "222"
},
{
"input": "5314 8",
"output": "4651"
},
{
"input": "15309 9",
"output": "13609"
},
{
"input": "35648 10",
"output": "32085"
},
{
"input": "3018012 8",
"output": "2640764"
},
{
"input": "92153348 9",
"output": "81914089"
},
{
"input": "177583558 10",
"output": "159825206"
},
{
"input": "1000000000 2",
"output": "500000008"
},
{
"input": "1000000000 3",
"output": "666666672"
},
{
"input": "1000000000 4",
"output": "750000005"
},
{
"input": "1000000000 5",
"output": "800000003"
},
{
"input": "1000000000 6",
"output": "833333338"
},
{
"input": "1000000000 7",
"output": "857142861"
},
{
"input": "1000000000 8",
"output": "875000004"
},
{
"input": "1000000000 9",
"output": "888888894"
},
{
"input": "1000000000 10",
"output": "900000001"
},
{
"input": "1 4",
"output": "1"
},
{
"input": "2 10",
"output": "2"
},
{
"input": "1 2",
"output": "1"
},
{
"input": "6 8",
"output": "6"
},
{
"input": "987862820 9",
"output": "878100288"
},
{
"input": "979591791 9",
"output": "870748262"
},
{
"input": "948889213 9",
"output": "843457081"
},
{
"input": "8 9",
"output": "8"
},
{
"input": "999999999 10",
"output": "900000000"
}
] | 92 | 4,505,600 | 3 | 1,794 |
|
542 | Idempotent functions | [
"constructive algorithms",
"graphs",
"math"
] | null | null | Some time ago Leonid have known about idempotent functions. Idempotent function defined on a set {1,<=2,<=...,<=*n*} is such function , that for any the formula *g*(*g*(*x*))<==<=*g*(*x*) holds.
Let's denote as *f*(*k*)(*x*) the function *f* applied *k* times to the value *x*. More formally, *f*(1)(*x*)<==<=*f*(*x*), *f*(*k*)(*x*)<==<=*f*(*f*(*k*<=-<=1)(*x*)) for each *k*<=><=1.
You are given some function . Your task is to find minimum positive integer *k* such that function *f*(*k*)(*x*) is idempotent. | In the first line of the input there is a single integer *n* (1<=β€<=*n*<=β€<=200) β the size of function *f* domain.
In the second line follow *f*(1),<=*f*(2),<=...,<=*f*(*n*) (1<=β€<=*f*(*i*)<=β€<=*n* for each 1<=β€<=*i*<=β€<=*n*), the values of a function. | Output minimum *k* such that function *f*(*k*)(*x*) is idempotent. | [
"4\n1 2 2 4\n",
"3\n2 3 3\n",
"3\n2 3 1\n"
] | [
"1\n",
"2\n",
"3\n"
] | In the first sample test function *f*(*x*)β=β*f*<sup class="upper-index">(1)</sup>(*x*) is already idempotent since *f*(*f*(1))β=β*f*(1)β=β1, *f*(*f*(2))β=β*f*(2)β=β2, *f*(*f*(3))β=β*f*(3)β=β2, *f*(*f*(4))β=β*f*(4)β=β4.
In the second sample test:
- function *f*(*x*)β=β*f*<sup class="upper-index">(1)</sup>(*x*) isn't idempotent because *f*(*f*(1))β=β3 but *f*(1)β=β2; - function *f*(*x*)β=β*f*<sup class="upper-index">(2)</sup>(*x*) is idempotent since for any *x* it is true that *f*<sup class="upper-index">(2)</sup>(*x*)β=β3, so it is also true that *f*<sup class="upper-index">(2)</sup>(*f*<sup class="upper-index">(2)</sup>(*x*))β=β3.
In the third sample test:
- function *f*(*x*)β=β*f*<sup class="upper-index">(1)</sup>(*x*) isn't idempotent because *f*(*f*(1))β=β3 but *f*(1)β=β2; - function *f*(*f*(*x*))β=β*f*<sup class="upper-index">(2)</sup>(*x*) isn't idempotent because *f*<sup class="upper-index">(2)</sup>(*f*<sup class="upper-index">(2)</sup>(1))β=β2 but *f*<sup class="upper-index">(2)</sup>(1)β=β3; - function *f*(*f*(*f*(*x*)))β=β*f*<sup class="upper-index">(3)</sup>(*x*) is idempotent since it is identity function: *f*<sup class="upper-index">(3)</sup>(*x*)β=β*x* for any <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/46a8c73444c646004dfde04451775e7af924d108.png" style="max-width: 100.0%;max-height: 100.0%;"/> meaning that the formula *f*<sup class="upper-index">(3)</sup>(*f*<sup class="upper-index">(3)</sup>(*x*))β=β*f*<sup class="upper-index">(3)</sup>(*x*) also holds. | [
{
"input": "4\n1 2 2 4",
"output": "1"
},
{
"input": "3\n2 3 3",
"output": "2"
},
{
"input": "3\n2 3 1",
"output": "3"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "16\n1 4 13 9 11 16 14 6 5 12 7 8 15 2 3 10",
"output": "105"
},
{
"input": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
"output": "1"
},
{
"input": "20\n11 14 2 10 17 5 9 6 18 3 17 7 4 15 17 1 4 14 10 11",
"output": "7"
},
{
"input": "100\n46 7 63 48 75 82 85 90 65 23 36 96 96 29 76 67 26 2 72 76 18 30 48 98 100 61 55 74 18 28 36 89 4 65 94 48 53 19 66 77 91 35 94 97 19 45 82 56 11 23 24 51 62 85 25 11 68 19 57 92 53 31 36 28 70 36 62 78 19 10 12 35 46 74 31 79 15 98 15 80 24 59 98 96 92 1 92 16 13 73 99 100 76 52 52 40 85 54 49 89",
"output": "24"
},
{
"input": "100\n61 41 85 52 22 82 98 25 60 35 67 78 65 69 55 86 34 91 92 36 24 2 26 15 76 99 4 95 79 31 13 16 100 83 21 90 73 32 19 33 77 40 72 62 88 43 84 14 10 9 46 70 23 45 42 96 94 38 97 58 47 93 59 51 57 7 27 74 1 30 64 3 63 49 50 54 5 37 48 11 81 44 12 17 75 71 89 39 56 20 6 8 53 28 80 66 29 87 18 68",
"output": "14549535"
},
{
"input": "2\n1 2",
"output": "1"
},
{
"input": "2\n1 1",
"output": "1"
},
{
"input": "2\n2 2",
"output": "1"
},
{
"input": "2\n2 1",
"output": "2"
},
{
"input": "5\n2 1 2 3 4",
"output": "4"
},
{
"input": "3\n2 1 2",
"output": "2"
},
{
"input": "4\n2 1 2 3",
"output": "2"
},
{
"input": "6\n2 1 2 3 4 5",
"output": "4"
},
{
"input": "4\n2 3 1 1",
"output": "3"
},
{
"input": "5\n2 3 1 1 4",
"output": "3"
},
{
"input": "6\n2 3 1 1 4 5",
"output": "3"
},
{
"input": "7\n2 3 1 1 4 5 6",
"output": "6"
},
{
"input": "8\n2 3 1 1 4 5 6 7",
"output": "6"
},
{
"input": "142\n131 32 130 139 5 11 36 2 39 92 111 91 8 14 65 82 90 72 140 80 26 124 97 15 43 77 58 132 21 68 31 45 6 69 70 79 141 27 125 78 93 88 115 104 17 55 86 28 56 117 121 136 12 59 85 95 74 18 87 22 106 112 60 119 81 66 52 14 25 127 29 103 24 48 126 30 120 107 51 47 133 129 96 138 113 37 64 114 53 73 108 62 1 123 63 57 142 76 16 4 35 54 19 110 42 116 7 10 118 9 71 49 75 23 89 99 3 137 38 98 61 128 102 13 122 33 50 94 100 105 109 134 40 20 135 46 34 41 83 67 44 84",
"output": "137"
},
{
"input": "142\n34 88 88 88 88 88 131 53 88 130 131 88 88 130 88 131 53 130 130 34 88 88 131 130 53 88 88 34 131 130 88 131 130 34 130 53 53 34 53 34 130 34 88 88 130 88 131 130 34 53 88 34 53 88 130 53 34 53 88 131 130 34 88 88 130 88 130 130 131 131 130 53 131 130 131 130 53 34 131 34 88 53 88 53 34 130 88 88 130 53 130 34 131 130 53 131 130 88 130 131 53 130 34 130 88 53 88 88 53 88 34 131 88 131 130 53 130 130 53 130 88 88 131 53 88 53 53 34 53 130 131 130 34 131 34 53 130 88 34 34 53 34",
"output": "1"
},
{
"input": "142\n25 46 7 30 112 34 76 5 130 122 7 132 54 82 139 97 79 112 79 79 112 43 25 50 118 112 87 11 51 30 90 56 119 46 9 81 5 103 78 18 49 37 43 129 124 90 109 6 31 50 90 20 79 99 130 31 131 62 50 84 5 34 6 41 79 112 9 30 141 114 34 11 46 92 97 30 95 112 24 24 74 121 65 31 127 28 140 30 79 90 9 10 56 88 9 65 128 79 56 37 109 37 30 95 37 105 3 102 120 18 28 90 107 29 128 137 59 62 62 77 34 43 26 5 99 97 44 130 115 130 130 47 83 53 77 80 131 79 28 98 10 52",
"output": "8"
},
{
"input": "142\n138 102 2 111 17 64 25 11 3 90 118 120 46 33 131 87 119 9 72 141 62 116 44 136 81 122 93 106 123 62 35 17 98 49 46 124 32 67 93 90 91 99 36 46 138 5 52 73 139 2 11 97 6 9 47 56 134 134 112 90 94 55 97 98 118 37 109 31 132 58 95 98 76 76 63 7 110 28 133 50 46 136 115 57 113 55 4 96 63 66 9 52 107 17 95 78 95 118 69 105 18 10 52 94 29 36 113 86 132 39 77 42 113 116 135 93 136 39 48 119 124 35 10 133 138 45 78 107 132 130 49 28 73 85 135 4 119 38 78 116 108 125",
"output": "20"
},
{
"input": "9\n7 3 8 9 9 3 5 3 2",
"output": "6"
},
{
"input": "5\n2 1 4 5 3",
"output": "6"
},
{
"input": "7\n2 3 4 5 6 7 6",
"output": "6"
},
{
"input": "129\n2 1 4 5 3 7 8 9 10 6 12 13 14 15 16 17 11 19 20 21 22 23 24 25 26 27 28 18 30 31 32 33 34 35 36 37 38 39 40 41 29 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 42 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 59 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 78 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 101",
"output": "6469693230"
},
{
"input": "4\n2 3 4 1",
"output": "4"
}
] | 1,000 | 0 | 0 | 1,796 |
|
0 | none | [
"none"
] | null | null | You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made *y* submissions, out of which *x* have been successful. Thus, your current success rate on Codeforces is equal to *x*<=/<=*y*.
Your favorite rational number in the [0;1] range is *p*<=/<=*q*. Now you wonder: what is the smallest number of submissions you have to make if you want your success rate to be *p*<=/<=*q*? | The first line contains a single integer *t* (1<=β€<=*t*<=β€<=1000)Β β the number of test cases.
Each of the next *t* lines contains four integers *x*, *y*, *p* and *q* (0<=β€<=*x*<=β€<=*y*<=β€<=109; 0<=β€<=*p*<=β€<=*q*<=β€<=109; *y*<=><=0; *q*<=><=0).
It is guaranteed that *p*<=/<=*q* is an irreducible fraction.
Hacks. For hacks, an additional constraint of *t*<=β€<=5 must be met. | For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve. | [
"4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1\n"
] | [
"4\n10\n0\n-1\n"
] | In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7β/β14, or 1β/β2.
In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9β/β24, or 3β/β8.
In the third example, there is no need to make any new submissions. Your success rate is already equal to 20β/β70, or 2β/β7.
In the fourth example, the only unsuccessful submission breaks your hopes of having the success rate equal to 1. | [
{
"input": "4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1",
"output": "4\n10\n0\n-1"
},
{
"input": "8\n0 1 0 1\n0 2 1 2\n0 3 1 1\n1 2 0 1\n1 2 1 1\n2 2 0 1\n3 3 1 2\n4 4 1 1",
"output": "0\n2\n-1\n-1\n-1\n-1\n3\n0"
},
{
"input": "5\n1 1000000000 1 2\n1 1000000000 1 2\n1 1000000000 1 2\n1 1000000000 1 2\n1 1000000000 1 2",
"output": "999999998\n999999998\n999999998\n999999998\n999999998"
},
{
"input": "5\n999999999 1000000000 1 1000000000\n999999999 1000000000 1 1000000000\n999999999 1000000000 1 1000000000\n999999999 1000000000 1 1000000000\n999999999 1000000000 1 1000000000",
"output": "999999998000000000\n999999998000000000\n999999998000000000\n999999998000000000\n999999998000000000"
},
{
"input": "5\n0 1000000000 999999999 1000000000\n0 1000000000 999999999 1000000000\n0 1000000000 999999999 1000000000\n0 1000000000 999999999 1000000000\n0 1000000000 999999999 1000000000",
"output": "999999999000000000\n999999999000000000\n999999999000000000\n999999999000000000\n999999999000000000"
},
{
"input": "1\n999999999 1000000000 1 2",
"output": "999999998"
},
{
"input": "1\n50 50 1 1",
"output": "0"
},
{
"input": "1\n100000000 100000000 1 2",
"output": "100000000"
},
{
"input": "1\n3 999999990 1 1000000000",
"output": "2000000010"
},
{
"input": "5\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1\n1 1 1 1",
"output": "4\n10\n0\n-1\n0"
},
{
"input": "5\n9999999 10000000 1 1000000000\n9999999 10000000 1 1000000000\n9999999 10000000 1 1000000000\n9999999 10000000 1 1000000000\n9999999 10000000 1 1000000000",
"output": "9999998990000000\n9999998990000000\n9999998990000000\n9999998990000000\n9999998990000000"
},
{
"input": "1\n0 1000000000 999999999 1000000000",
"output": "999999999000000000"
},
{
"input": "5\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000",
"output": "999999998000000000\n999999998000000000\n999999998000000000\n999999998000000000\n999999998000000000"
},
{
"input": "5\n1 1000000000 999999999 1000000000\n2 1000000000 999999999 1000000000\n3 1000000000 999999999 1000000000\n4 1000000000 999999999 1000000000\n5 1000000000 999999999 1000000000",
"output": "999999998000000000\n999999997000000000\n999999996000000000\n999999995000000000\n999999994000000000"
},
{
"input": "1\n1 1 1 1",
"output": "0"
},
{
"input": "5\n999999997 999999998 2 999999999\n999999997 999999998 2 999999999\n999999997 999999998 2 999999999\n999999997 999999998 2 999999999\n999999997 999999998 2 999999999",
"output": "499999997500000003\n499999997500000003\n499999997500000003\n499999997500000003\n499999997500000003"
},
{
"input": "5\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000",
"output": "999999999000000000\n999999999000000000\n999999999000000000\n999999999000000000\n999999999000000000"
},
{
"input": "5\n99999997 999999998 999999998 999999999\n99999996 999999997 999999997 999999999\n99999997 999999998 999999998 999999999\n99999996 999999997 999999997 999999999\n99999997 999999998 999999998 999999999",
"output": "899999999100000001\n449999999550000002\n899999999100000001\n449999999550000002\n899999999100000001"
},
{
"input": "1\n1000000000 1000000000 1 1000000000",
"output": "999999999000000000"
},
{
"input": "1\n7 7 1 1",
"output": "0"
},
{
"input": "5\n1000000000 1000000000 1 2\n1000000000 1000000000 1 2\n1000000000 1000000000 1 2\n1000000000 1000000000 1 2\n1000000000 1000000000 1 2",
"output": "1000000000\n1000000000\n1000000000\n1000000000\n1000000000"
},
{
"input": "1\n1000000000 1000000000 1 1",
"output": "0"
},
{
"input": "1\n1 1000000000 999999999 1000000000",
"output": "999999998000000000"
},
{
"input": "4\n1 1000000000 999999999 1000000000\n999999999 1000000000 1 1000000000\n1 2 0 1\n0 1 0 1",
"output": "999999998000000000\n999999998000000000\n-1\n0"
},
{
"input": "1\n1 1000000000 1 2",
"output": "999999998"
},
{
"input": "5\n1 982449707 1 2\n1 982449707 1 2\n1 982449707 1 2\n1 982449707 1 2\n1 982449707 1 2",
"output": "982449705\n982449705\n982449705\n982449705\n982449705"
},
{
"input": "5\n13 900000007 900000007 900000009\n13 900000007 900000007 900000009\n13 900000007 900000007 900000009\n13 900000007 900000007 900000009\n13 900000007 900000007 900000009",
"output": "405000000449999966\n405000000449999966\n405000000449999966\n405000000449999966\n405000000449999966"
},
{
"input": "1\n5 10 0 1",
"output": "-1"
},
{
"input": "1\n2 2 1 1",
"output": "0"
},
{
"input": "5\n0 999999999 999999999 1000000000\n0 999999999 999999999 1000000000\n0 999999999 999999999 1000000000\n0 999999999 999999999 1000000000\n0 999999999 999999999 1000000000",
"output": "999999998000000001\n999999998000000001\n999999998000000001\n999999998000000001\n999999998000000001"
},
{
"input": "1\n0 5 0 1",
"output": "0"
},
{
"input": "5\n999999999 1000000000 1 9999\n999999999 1000000000 1 9999\n999999999 1000000000 1 9999\n999999999 1000000000 1 9999\n999999999 1000000000 1 9999",
"output": "9997999990001\n9997999990001\n9997999990001\n9997999990001\n9997999990001"
},
{
"input": "5\n999999997 1000000000 3 1000000000\n999999997 1000000000 3 1000000000\n999999997 1000000000 3 1000000000\n999999997 1000000000 3 1000000000\n999999997 1000000000 3 1000000000",
"output": "333333332000000000\n333333332000000000\n333333332000000000\n333333332000000000\n333333332000000000"
},
{
"input": "5\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000",
"output": "999999999000000000\n999999999000000000\n999999998000000000\n999999998000000000\n999999998000000000"
},
{
"input": "5\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000\n1000000000 1000000000 1 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000",
"output": "999999999000000000\n999999999000000000\n999999999000000000\n999999998000000000\n999999998000000000"
},
{
"input": "1\n999999998 999999999 1 10",
"output": "8999999981"
},
{
"input": "5\n1 1 1 1\n2 2 1 1\n100 100 1 1\n1000000000 1000000000 1 1\n1000000000 1000000000 1 1",
"output": "0\n0\n0\n0\n0"
},
{
"input": "4\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000\n1 1000000000 999999999 1000000000",
"output": "999999998000000000\n999999998000000000\n999999998000000000\n999999998000000000"
}
] | 280 | 6,246,400 | 3 | 1,799 |
|
938 | Word Correction | [
"implementation"
] | null | null | Victor tries to write his own text editor, with word correction included. However, the rules of word correction are really strange.
Victor thinks that if a word contains two consecutive vowels, then it's kinda weird and it needs to be replaced. So the word corrector works in such a way: as long as there are two consecutive vowels in the word, it deletes the first vowel in a word such that there is another vowel right before it. If there are no two consecutive vowels in the word, it is considered to be correct.
You are given a word *s*. Can you predict what will it become after correction?
In this problem letters a, e, i, o, u and y are considered to be vowels. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100) β the number of letters in word *s* before the correction.
The second line contains a string *s* consisting of exactly *n* lowercase Latin letters β the word before the correction. | Output the word *s* after the correction. | [
"5\nweird\n",
"4\nword\n",
"5\naaeaa\n"
] | [
"werd\n",
"word\n",
"a\n"
] | Explanations of the examples:
1. There is only one replace: weird <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> werd;1. No replace needed since there are no two consecutive vowels;1. aaeaa <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> aeaa <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> aaa <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> aa <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> a. | [
{
"input": "5\nweird",
"output": "werd"
},
{
"input": "4\nword",
"output": "word"
},
{
"input": "5\naaeaa",
"output": "a"
},
{
"input": "100\naaaaabbbbboyoyoyoyoyacadabbbbbiuiufgiuiuaahjabbbklboyoyoyoyoyaaaaabbbbbiuiuiuiuiuaaaaabbbbbeyiyuyzyw",
"output": "abbbbbocadabbbbbifgihjabbbklbobbbbbibbbbbezyw"
},
{
"input": "69\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "12\nmmmmmmmmmmmm",
"output": "mmmmmmmmmmmm"
},
{
"input": "18\nyaywptqwuyiqypwoyw",
"output": "ywptqwuqypwow"
},
{
"input": "85\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "13\nmmmmmmmmmmmmm",
"output": "mmmmmmmmmmmmm"
},
{
"input": "10\nmmmmmmmmmm",
"output": "mmmmmmmmmm"
},
{
"input": "11\nmmmmmmmmmmm",
"output": "mmmmmmmmmmm"
},
{
"input": "15\nmmmmmmmmmmmmmmm",
"output": "mmmmmmmmmmmmmmm"
},
{
"input": "1\na",
"output": "a"
},
{
"input": "14\nmmmmmmmmmmmmmm",
"output": "mmmmmmmmmmmmmm"
},
{
"input": "33\nmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
"output": "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
},
{
"input": "79\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "90\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "2\naa",
"output": "a"
},
{
"input": "18\niuiuqpyyaoaetiwliu",
"output": "iqpytiwli"
},
{
"input": "5\nxxxxx",
"output": "xxxxx"
},
{
"input": "6\nxxxahg",
"output": "xxxahg"
},
{
"input": "3\nzcv",
"output": "zcv"
},
{
"input": "4\naepo",
"output": "apo"
},
{
"input": "5\nqqqqq",
"output": "qqqqq"
},
{
"input": "6\naaaaaa",
"output": "a"
},
{
"input": "4\naeta",
"output": "ata"
},
{
"input": "20\nttyttlwaoieulyiluuri",
"output": "ttyttlwalyluri"
},
{
"input": "1\nb",
"output": "b"
},
{
"input": "3\nanc",
"output": "anc"
},
{
"input": "1\ne",
"output": "e"
},
{
"input": "3\naie",
"output": "a"
},
{
"input": "3\nvio",
"output": "vi"
},
{
"input": "2\nea",
"output": "e"
},
{
"input": "3\nuas",
"output": "us"
},
{
"input": "2\nba",
"output": "ba"
},
{
"input": "2\naq",
"output": "aq"
},
{
"input": "2\nya",
"output": "y"
},
{
"input": "2\nou",
"output": "o"
},
{
"input": "2\nbb",
"output": "bb"
},
{
"input": "7\nayylmao",
"output": "alma"
},
{
"input": "2\nab",
"output": "ab"
},
{
"input": "19\nyuouiyaoiiweqrryqqp",
"output": "yweqrryqqp"
},
{
"input": "25\niqypwqpriiioetiuqqqttouei",
"output": "iqypwqpritiqqqtto"
},
{
"input": "100\naaaaabbbbboyoyoyoyoyacadabbbbbiuiufgiuiuaahjabbbklboyoyoyoyoyaaaaabbbbbiuiuiuiuiuaaaaabbbbbeyiyuyzyz",
"output": "abbbbbocadabbbbbifgihjabbbklbobbbbbibbbbbezyz"
},
{
"input": "17\naccccccccccccccca",
"output": "accccccccccccccca"
},
{
"input": "5\nababa",
"output": "ababa"
},
{
"input": "10\naaaaaaaaaa",
"output": "a"
},
{
"input": "22\naaaaabbbbboyoyoyoyoyac",
"output": "abbbbboc"
},
{
"input": "7\nmahmoud",
"output": "mahmod"
}
] | 62 | 307,200 | 3 | 1,800 |
|
779 | Dishonest Sellers | [
"constructive algorithms",
"greedy",
"sortings"
] | null | null | Igor found out discounts in a shop and decided to buy *n* items. Discounts at the store will last for a week and Igor knows about each item that its price now is *a**i*, and after a week of discounts its price will be *b**i*.
Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.
Igor decided that buy at least *k* of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all *n* items. | In the first line there are two positive integer numbers *n* and *k* (1<=β€<=*n*<=β€<=2Β·105, 0<=β€<=*k*<=β€<=*n*) β total number of items to buy and minimal number of items Igor wants to by right now.
The second line contains sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=104) β prices of items during discounts (i.e. right now).
The third line contains sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=104) β prices of items after discounts (i.e. after a week). | Print the minimal amount of money Igor will spend to buy all *n* items. Remember, he should buy at least *k* items right now. | [
"3 1\n5 4 6\n3 1 5\n",
"5 3\n3 4 7 10 3\n4 5 5 12 5\n"
] | [
"10\n",
"25\n"
] | In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6β+β3β+β1β=β10.
In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3β+β4β+β10β+β3β+β5β=β25. | [
{
"input": "3 1\n5 4 6\n3 1 5",
"output": "10"
},
{
"input": "5 3\n3 4 7 10 3\n4 5 5 12 5",
"output": "25"
},
{
"input": "1 0\n9\n8",
"output": "8"
},
{
"input": "2 0\n4 10\n1 2",
"output": "3"
},
{
"input": "4 2\n19 5 17 13\n3 18 8 10",
"output": "29"
},
{
"input": "5 3\n28 17 20 45 45\n39 12 41 27 9",
"output": "101"
},
{
"input": "10 5\n87 96 19 81 10 88 7 49 36 21\n11 75 28 28 74 17 64 19 81 31",
"output": "243"
},
{
"input": "50 45\n80 125 152 122 85 62 123 195 157 73 8 127 47 193 70 132 131 38 98 97 185 98 68 166 114 53 144 106 141 106 191 123 44 199 35 18 84 169 46 52 129 102 155 130 63 169 116 145 154 41\n155 187 79 106 136 173 17 146 9 47 173 21 37 183 110 74 185 14 82 156 147 25 162 195 26 25 22 80 171 180 7 92 53 18 100 17 159 184 37 127 116 192 163 49 94 95 118 102 8 30",
"output": "4680"
},
{
"input": "5 2\n1 1 1 1 1\n2 2 2 2 2",
"output": "5"
},
{
"input": "5 2\n2 4 4 4 4\n3 1 1 1 1",
"output": "9"
},
{
"input": "6 3\n9 9 9 2 2 2\n6 6 6 1 1 1",
"output": "24"
},
{
"input": "5 3\n10 17 23 26 35\n7 18 18 29 32",
"output": "103"
},
{
"input": "2 1\n10 12\n5 6",
"output": "16"
}
] | 124 | 307,200 | -1 | 1,803 |
|
962 | Make a Square | [
"brute force",
"implementation",
"math"
] | null | null | You are given a positive integer $n$, written without leading zeroes (for example, the number 04 is incorrect).
In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros.
Determine the minimum number of operations that you need to consistently apply to the given integer $n$ to make from it the square of some positive integer or report that it is impossible.
An integer $x$ is the square of some positive integer if and only if $x=y^2$ for some positive integer $y$. | The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^{9}$). The number is given without leading zeroes. | If it is impossible to make the square of some positive integer from $n$, print -1. In the other case, print the minimal number of operations required to do it. | [
"8314\n",
"625\n",
"333\n"
] | [
"2\n",
"0\n",
"-1\n"
] | In the first example we should delete from $8314$ the digits $3$ and $4$. After that $8314$ become equals to $81$, which is the square of the integer $9$.
In the second example the given $625$ is the square of the integer $25$, so you should not delete anything.
In the third example it is impossible to make the square from $333$, so the answer is -1. | [
{
"input": "8314",
"output": "2"
},
{
"input": "625",
"output": "0"
},
{
"input": "333",
"output": "-1"
},
{
"input": "1881388645",
"output": "6"
},
{
"input": "1059472069",
"output": "3"
},
{
"input": "1354124829",
"output": "4"
},
{
"input": "149723943",
"output": "4"
},
{
"input": "101",
"output": "2"
},
{
"input": "1999967841",
"output": "0"
},
{
"input": "2000000000",
"output": "-1"
},
{
"input": "1999431225",
"output": "0"
},
{
"input": "30",
"output": "-1"
},
{
"input": "1000",
"output": "1"
},
{
"input": "3081",
"output": "2"
},
{
"input": "10",
"output": "1"
},
{
"input": "2003064",
"output": "3"
},
{
"input": "701",
"output": "2"
},
{
"input": "1234567891",
"output": "4"
},
{
"input": "10625",
"output": "2"
},
{
"input": "13579",
"output": "4"
},
{
"input": "1999999999",
"output": "9"
},
{
"input": "150000",
"output": "1"
},
{
"input": "8010902",
"output": "3"
},
{
"input": "20100",
"output": "2"
},
{
"input": "40404",
"output": "2"
},
{
"input": "70000729",
"output": "5"
},
{
"input": "1899933124",
"output": "5"
},
{
"input": "1999999081",
"output": "8"
},
{
"input": "326700",
"output": "2"
},
{
"input": "1",
"output": "0"
},
{
"input": "1000000990",
"output": "3"
},
{
"input": "10000",
"output": "0"
},
{
"input": "100001",
"output": "1"
},
{
"input": "1410065408",
"output": "7"
},
{
"input": "1409865409",
"output": "5"
},
{
"input": "1000050001",
"output": "3"
},
{
"input": "1044435556",
"output": "2"
},
{
"input": "520993450",
"output": "6"
},
{
"input": "131073",
"output": "5"
},
{
"input": "500040004",
"output": "6"
},
{
"input": "237555493",
"output": "7"
},
{
"input": "1120671621",
"output": "5"
},
{
"input": "298755045",
"output": "5"
},
{
"input": "1476838469",
"output": "5"
},
{
"input": "654921893",
"output": "4"
},
{
"input": "1538038021",
"output": "4"
},
{
"input": "716121445",
"output": "6"
},
{
"input": "1894204869",
"output": "5"
},
{
"input": "1800098866",
"output": "7"
},
{
"input": "890665277",
"output": "8"
},
{
"input": "1686264392",
"output": "6"
},
{
"input": "1336639314",
"output": "6"
},
{
"input": "132238429",
"output": "5"
},
{
"input": "927837544",
"output": "4"
},
{
"input": "18403955",
"output": "4"
},
{
"input": "1668778878",
"output": "8"
},
{
"input": "2",
"output": "-1"
},
{
"input": "3",
"output": "-1"
},
{
"input": "4",
"output": "0"
},
{
"input": "5",
"output": "-1"
},
{
"input": "6",
"output": "-1"
},
{
"input": "7",
"output": "-1"
},
{
"input": "8",
"output": "-1"
},
{
"input": "9",
"output": "0"
},
{
"input": "11",
"output": "1"
},
{
"input": "12",
"output": "1"
},
{
"input": "13",
"output": "1"
},
{
"input": "14",
"output": "1"
},
{
"input": "15",
"output": "1"
},
{
"input": "16",
"output": "0"
}
] | 109 | 2,457,600 | 0 | 1,804 |
|
285 | Building Permutation | [
"greedy",
"implementation",
"sortings"
] | null | null | Permutation *p* is an ordered set of integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1,<=<=*p*2,<=<=...,<=<=*p**n*.
You have a sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. In one move, you are allowed to decrease or increase any number by one. Count the minimum number of moves, needed to build a permutation from this sequence. | The first line contains integer *n* (1<=β€<=*n*<=β€<=3Β·105) β the size of the sought permutation. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109). | Print a single number β the minimum number of moves.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"2\n3 0\n",
"3\n-1 -1 2\n"
] | [
"2\n",
"6\n"
] | In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2,β1).
In the second sample you need 6 moves to build permutation (1,β3,β2). | [
{
"input": "2\n3 0",
"output": "2"
},
{
"input": "3\n-1 -1 2",
"output": "6"
},
{
"input": "5\n-3 5 -3 3 3",
"output": "10"
},
{
"input": "10\n9 6 -2 4 1 1 1 9 6 2",
"output": "18"
},
{
"input": "9\n2 0 0 6 5 4 1 9 3",
"output": "15"
},
{
"input": "100\n-77 57 -95 -23 53 -28 82 -83 38 -73 85 28 25 6 -43 4 -10 -30 -9 -92 14 34 -93 61 36 -100 90 -68 28 16 100 -3 97 30 36 -55 62 -62 53 74 -50 -23 67 11 22 -30 -19 83 7 84 43 90 -65 -75 -15 97 90 15 66 2 13 -91 91 -44 46 51 51 -58 95 77 20 30 76 79 91 60 76 2 82 42 -93 94 -57 88 65 -95 -66 100 -9 33 -67 54 -99 97 53 13 54 66 60 -48",
"output": "3459"
}
] | 467 | 31,948,800 | 3 | 1,807 |
|
975 | Valhalla Siege | [
"binary search"
] | null | null | Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle.
Ivar has $n$ warriors, he places them on a straight line in front of the main gate, in a way that the $i$-th warrior stands right after $(i-1)$-th warrior. The first warrior leads the attack.
Each attacker can take up to $a_i$ arrows before he falls to the ground, where $a_i$ is the $i$-th warrior's strength.
Lagertha orders her warriors to shoot $k_i$ arrows during the $i$-th minute, the arrows one by one hit the first still standing warrior. After all Ivar's warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar's warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute $t$, they will all be standing to fight at the end of minute $t$.
The battle will last for $q$ minutes, after each minute you should tell Ivar what is the number of his standing warriors. | The first line contains two integers $n$ and $q$ ($1 \le n, q \leq 200\,000$)Β β the number of warriors and the number of minutes in the battle.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) that represent the warriors' strengths.
The third line contains $q$ integers $k_1, k_2, \ldots, k_q$ ($1 \leq k_i \leq 10^{14}$), the $i$-th of them represents Lagertha's order at the $i$-th minute: $k_i$ arrows will attack the warriors. | Output $q$ lines, the $i$-th of them is the number of standing warriors after the $i$-th minute. | [
"5 5\n1 2 1 2 1\n3 10 1 1 1\n",
"4 4\n1 2 3 4\n9 1 10 6\n"
] | [
"3\n5\n4\n4\n3\n",
"1\n4\n4\n1\n"
] | In the first example:
- after the 1-st minute, the 1-st and 2-nd warriors die. - after the 2-nd minute all warriors die (and all arrows left over are wasted), then they will be revived thus answer is 5Β β all warriors are alive. - after the 3-rd minute, the 1-st warrior dies. - after the 4-th minute, the 2-nd warrior takes a hit and his strength decreases by 1. - after the 5-th minute, the 2-nd warrior dies. | [
{
"input": "5 5\n1 2 1 2 1\n3 10 1 1 1",
"output": "3\n5\n4\n4\n3"
},
{
"input": "4 4\n1 2 3 4\n9 1 10 6",
"output": "1\n4\n4\n1"
},
{
"input": "10 3\n1 1 1 1 1 1 1 1 1 1\n10 10 5",
"output": "10\n10\n5"
},
{
"input": "1 1\n56563128\n897699770",
"output": "1"
},
{
"input": "100 55\n1 2 4 4 3 5 5 2 3 4 2 1 1 2 3 5 1 5 4 2 5 4 4 3 3 5 3 4 4 5 5 2 3 3 4 4 3 4 5 5 5 5 3 5 1 2 4 3 4 5 3 3 2 1 4 5 3 4 4 1 5 1 5 2 2 1 4 5 3 3 1 4 2 5 4 5 3 2 5 5 2 3 2 3 2 2 3 4 4 4 1 4 2 4 5 3 1 3 3 1\n5 2 1 4 3 4 3 1 4 4 1 2 3 2 1 5 5 4 5 4 2 5 2 1 5 1 4 4 3 5 4 5 1 4 4 1 5 3 1 5 2 4 1 3 2 5 4 5 4 3 4 2 2 4 3",
"output": "98\n97\n97\n96\n95\n94\n94\n94\n92\n91\n91\n90\n87\n86\n86\n85\n83\n82\n80\n80\n79\n78\n77\n77\n75\n75\n74\n73\n72\n71\n70\n69\n69\n67\n66\n66\n65\n64\n63\n62\n62\n61\n61\n60\n60\n59\n58\n57\n54\n54\n52\n52\n51\n51\n50"
}
] | 2,000 | 3,993,600 | 0 | 1,810 |
|
980 | Marlin | [
"constructive algorithms"
] | null | null | The city of Fishtopia can be imagined as a grid of $4$ rows and an odd number of columns. It has two main villages; the first is located at the top-left cell $(1,1)$, people who stay there love fishing at the Tuna pond at the bottom-right cell $(4, n)$. The second village is located at $(4, 1)$ and its people love the Salmon pond at $(1, n)$.
The mayor of Fishtopia wants to place $k$ hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.
A person can move from one cell to another if those cells are not occupied by hotels and share a side.
Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond? | The first line of input contain two integers, $n$ and $k$ ($3 \leq n \leq 99$, $0 \leq k \leq 2\times(n-2)$), $n$ is odd, the width of the city, and the number of hotels to be placed, respectively. | Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO".
If it is possible, print an extra $4$ lines that describe the city, each line should have $n$ characters, each of which is "#" if that cell has a hotel on it, or "." if not. | [
"7 2\n",
"5 3\n"
] | [
"YES\n.......\n.#.....\n.#.....\n.......\n",
"YES\n.....\n.###.\n.....\n.....\n"
] | none | [
{
"input": "7 2",
"output": "YES\n.......\n.#.....\n.#.....\n......."
},
{
"input": "5 3",
"output": "YES\n.....\n.###.\n.....\n....."
},
{
"input": "3 2",
"output": "YES\n...\n.#.\n.#.\n..."
},
{
"input": "3 0",
"output": "YES\n...\n...\n...\n..."
},
{
"input": "49 1",
"output": "YES\n.................................................\n........................#........................\n.................................................\n................................................."
},
{
"input": "9 4",
"output": "YES\n.........\n.##......\n.##......\n........."
},
{
"input": "9 5",
"output": "YES\n.........\n.#.#.....\n.###.....\n........."
},
{
"input": "99 193",
"output": "YES\n...................................................................................................\n.###############################################################################################.#.\n.#################################################################################################.\n..................................................................................................."
},
{
"input": "99 14",
"output": "YES\n...................................................................................................\n.#######...........................................................................................\n.#######...........................................................................................\n..................................................................................................."
},
{
"input": "57 15",
"output": "YES\n.........................................................\n.######.#................................................\n.########................................................\n........................................................."
},
{
"input": "99 3",
"output": "YES\n...................................................................................................\n................................................###................................................\n...................................................................................................\n..................................................................................................."
},
{
"input": "3 1",
"output": "YES\n...\n.#.\n...\n..."
},
{
"input": "9 9",
"output": "YES\n.........\n.###.#...\n.#####...\n........."
},
{
"input": "67 9",
"output": "YES\n...................................................................\n.###.#.............................................................\n.#####.............................................................\n..................................................................."
},
{
"input": "99 99",
"output": "YES\n...................................................................................................\n.################################################.#................................................\n.##################################################................................................\n..................................................................................................."
},
{
"input": "31 32",
"output": "YES\n...............................\n.################..............\n.################..............\n..............................."
},
{
"input": "5 1",
"output": "YES\n.....\n..#..\n.....\n....."
},
{
"input": "5 2",
"output": "YES\n.....\n.#...\n.#...\n....."
},
{
"input": "5 4",
"output": "YES\n.....\n.##..\n.##..\n....."
},
{
"input": "5 6",
"output": "YES\n.....\n.###.\n.###.\n....."
},
{
"input": "5 5",
"output": "YES\n.....\n.#.#.\n.###.\n....."
},
{
"input": "7 9",
"output": "YES\n.......\n.###.#.\n.#####.\n......."
},
{
"input": "7 10",
"output": "YES\n.......\n.#####.\n.#####.\n......."
},
{
"input": "19 12",
"output": "YES\n...................\n.######............\n.######............\n..................."
},
{
"input": "19 3",
"output": "YES\n...................\n........###........\n...................\n..................."
},
{
"input": "37 14",
"output": "YES\n.....................................\n.#######.............................\n.#######.............................\n....................................."
},
{
"input": "37 15",
"output": "YES\n.....................................\n.######.#............................\n.########............................\n....................................."
},
{
"input": "37 37",
"output": "YES\n.....................................\n.#################.#.................\n.###################.................\n....................................."
},
{
"input": "37 36",
"output": "YES\n.....................................\n.##################..................\n.##################..................\n....................................."
},
{
"input": "37 35",
"output": "YES\n.....................................\n.################.#..................\n.##################..................\n....................................."
},
{
"input": "37 34",
"output": "YES\n.....................................\n.#################...................\n.#################...................\n....................................."
},
{
"input": "37 38",
"output": "YES\n.....................................\n.###################.................\n.###################.................\n....................................."
},
{
"input": "37 39",
"output": "YES\n.....................................\n.##################.#................\n.####################................\n....................................."
},
{
"input": "37 40",
"output": "YES\n.....................................\n.####################................\n.####################................\n....................................."
},
{
"input": "5 0",
"output": "YES\n.....\n.....\n.....\n....."
},
{
"input": "67 1",
"output": "YES\n...................................................................\n.................................#.................................\n...................................................................\n..................................................................."
},
{
"input": "37 19",
"output": "YES\n.....................................\n.########.#..........................\n.##########..........................\n....................................."
},
{
"input": "77 7",
"output": "YES\n.............................................................................\n.##.#........................................................................\n.####........................................................................\n............................................................................."
},
{
"input": "33 47",
"output": "YES\n.................................\n.######################.#........\n.########################........\n................................."
},
{
"input": "33 48",
"output": "YES\n.................................\n.########################........\n.########################........\n................................."
},
{
"input": "23 40",
"output": "YES\n.......................\n.####################..\n.####################..\n......................."
},
{
"input": "23 39",
"output": "YES\n.......................\n.##################.#..\n.####################..\n......................."
},
{
"input": "49 3",
"output": "YES\n.................................................\n.......................###.......................\n.................................................\n................................................."
},
{
"input": "99 1",
"output": "YES\n...................................................................................................\n.................................................#.................................................\n...................................................................................................\n..................................................................................................."
},
{
"input": "77 0",
"output": "YES\n.............................................................................\n.............................................................................\n.............................................................................\n............................................................................."
},
{
"input": "99 0",
"output": "YES\n...................................................................................................\n...................................................................................................\n...................................................................................................\n..................................................................................................."
},
{
"input": "99 5",
"output": "YES\n...................................................................................................\n.#.#...............................................................................................\n.###...............................................................................................\n..................................................................................................."
},
{
"input": "99 4",
"output": "YES\n...................................................................................................\n.##................................................................................................\n.##................................................................................................\n..................................................................................................."
},
{
"input": "99 20",
"output": "YES\n...................................................................................................\n.##########........................................................................................\n.##########........................................................................................\n..................................................................................................."
},
{
"input": "99 194",
"output": "YES\n...................................................................................................\n.#################################################################################################.\n.#################################################################################################.\n..................................................................................................."
},
{
"input": "99 192",
"output": "YES\n...................................................................................................\n.################################################################################################..\n.################################################################################################..\n..................................................................................................."
},
{
"input": "99 190",
"output": "YES\n...................................................................................................\n.###############################################################################################...\n.###############################################################################################...\n..................................................................................................."
},
{
"input": "99 189",
"output": "YES\n...................................................................................................\n.#############################################################################################.#...\n.###############################################################################################...\n..................................................................................................."
},
{
"input": "99 177",
"output": "YES\n...................................................................................................\n.#######################################################################################.#.........\n.#########################################################################################.........\n..................................................................................................."
},
{
"input": "99 154",
"output": "YES\n...................................................................................................\n.#############################################################################.....................\n.#############################################################################.....................\n..................................................................................................."
},
{
"input": "99 127",
"output": "YES\n...................................................................................................\n.##############################################################.#..................................\n.################################################################..................................\n..................................................................................................."
},
{
"input": "99 55",
"output": "YES\n...................................................................................................\n.##########################.#......................................................................\n.############################......................................................................\n..................................................................................................."
},
{
"input": "99 40",
"output": "YES\n...................................................................................................\n.####################..............................................................................\n.####################..............................................................................\n..................................................................................................."
},
{
"input": "97 190",
"output": "YES\n.................................................................................................\n.###############################################################################################.\n.###############################################################################################.\n................................................................................................."
},
{
"input": "97 100",
"output": "YES\n.................................................................................................\n.##################################################..............................................\n.##################################################..............................................\n................................................................................................."
},
{
"input": "97 111",
"output": "YES\n.................................................................................................\n.######################################################.#........................................\n.########################################################........................................\n................................................................................................."
},
{
"input": "97 64",
"output": "YES\n.................................................................................................\n.################################................................................................\n.################################................................................................\n................................................................................................."
},
{
"input": "97 77",
"output": "YES\n.................................................................................................\n.#####################################.#.........................................................\n.#######################################.........................................................\n................................................................................................."
},
{
"input": "91 77",
"output": "YES\n...........................................................................................\n.#####################################.#...................................................\n.#######################################...................................................\n..........................................................................................."
},
{
"input": "91 128",
"output": "YES\n...........................................................................................\n.################################################################..........................\n.################################################################..........................\n..........................................................................................."
},
{
"input": "91 113",
"output": "YES\n...........................................................................................\n.#######################################################.#.................................\n.#########################################################.................................\n..........................................................................................."
},
{
"input": "55 55",
"output": "YES\n.......................................................\n.##########################.#..........................\n.############################..........................\n......................................................."
},
{
"input": "43 34",
"output": "YES\n...........................................\n.#################.........................\n.#################.........................\n..........................................."
},
{
"input": "13 21",
"output": "YES\n.............\n.#########.#.\n.###########.\n............."
},
{
"input": "27 50",
"output": "YES\n...........................\n.#########################.\n.#########################.\n..........................."
},
{
"input": "27 49",
"output": "YES\n...........................\n.#######################.#.\n.#########################.\n..........................."
},
{
"input": "27 48",
"output": "YES\n...........................\n.########################..\n.########################..\n..........................."
},
{
"input": "27 40",
"output": "YES\n...........................\n.####################......\n.####################......\n..........................."
},
{
"input": "87 80",
"output": "YES\n.......................................................................................\n.########################################..............................................\n.########################################..............................................\n......................................................................................."
},
{
"input": "69 17",
"output": "YES\n.....................................................................\n.#######.#...........................................................\n.#########...........................................................\n....................................................................."
},
{
"input": "39 73",
"output": "YES\n.......................................\n.###################################.#.\n.#####################################.\n......................................."
},
{
"input": "39 72",
"output": "YES\n.......................................\n.####################################..\n.####################################..\n......................................."
},
{
"input": "33 57",
"output": "YES\n.................................\n.###########################.#...\n.#############################...\n................................."
},
{
"input": "33 54",
"output": "YES\n.................................\n.###########################.....\n.###########################.....\n................................."
},
{
"input": "99 2",
"output": "YES\n...................................................................................................\n.#.................................................................................................\n.#.................................................................................................\n..................................................................................................."
},
{
"input": "99 7",
"output": "YES\n...................................................................................................\n.##.#..............................................................................................\n.####..............................................................................................\n..................................................................................................."
},
{
"input": "99 37",
"output": "YES\n...................................................................................................\n.#################.#...............................................................................\n.###################...............................................................................\n..................................................................................................."
},
{
"input": "57 67",
"output": "YES\n.........................................................\n.################################.#......................\n.##################################......................\n........................................................."
},
{
"input": "57 34",
"output": "YES\n.........................................................\n.#################.......................................\n.#################.......................................\n........................................................."
},
{
"input": "73 17",
"output": "YES\n.........................................................................\n.#######.#...............................................................\n.#########...............................................................\n........................................................................."
},
{
"input": "73 43",
"output": "YES\n.........................................................................\n.####################.#..................................................\n.######################..................................................\n........................................................................."
},
{
"input": "7 3",
"output": "YES\n.......\n..###..\n.......\n......."
},
{
"input": "7 7",
"output": "YES\n.......\n.##.#..\n.####..\n......."
}
] | 46 | 0 | 0 | 1,819 |
|
796 | Find The Bone | [
"implementation"
] | null | null | Zane the wizard is going to perform a magic show shuffling the cups.
There are *n* cups, numbered from 1 to *n*, placed along the *x*-axis on a table that has *m* holes on it. More precisely, cup *i* is on the table at the position *x*<==<=*i*.
The problematic bone is initially at the position *x*<==<=1. Zane will confuse the audience by swapping the cups *k* times, the *i*-th time of which involves the cups at the positions *x*<==<=*u**i* and *x*<==<=*v**i*. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.
Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at *x*<==<=4 and the one at *x*<==<=6, they will not be at the position *x*<==<=5 at any moment during the operation.
Zaneβs puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone. | The first line contains three integers *n*, *m*, and *k* (2<=β€<=*n*<=β€<=106, 1<=β€<=*m*<=β€<=*n*, 1<=β€<=*k*<=β€<=3Β·105)Β β the number of cups, the number of holes on the table, and the number of swapping operations, respectively.
The second line contains *m* distinct integers *h*1,<=*h*2,<=...,<=*h**m* (1<=β€<=*h**i*<=β€<=*n*)Β β the positions along the *x*-axis where there is a hole on the table.
Each of the next *k* lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the positions of the cups to be swapped. | Print one integerΒ β the final position along the *x*-axis of the bone. | [
"7 3 4\n3 4 6\n1 2\n2 5\n5 7\n7 1\n",
"5 1 2\n2\n1 2\n2 4\n"
] | [
"1",
"2"
] | In the first sample, after the operations, the bone becomes at *x*β=β2, *x*β=β5, *x*β=β7, and *x*β=β1, respectively.
In the second sample, after the first operation, the bone becomes at *x*β=β2, and falls into the hole onto the ground. | [
{
"input": "7 3 4\n3 4 6\n1 2\n2 5\n5 7\n7 1",
"output": "1"
},
{
"input": "5 1 2\n2\n1 2\n2 4",
"output": "2"
},
{
"input": "10000 1 9\n55\n44 1\n2929 9292\n9999 9998\n44 55\n49 94\n55 53\n100 199\n55 50\n53 11",
"output": "55"
},
{
"input": "100000 3 7\n2 3 4\n1 5\n5 1\n1 5\n5 1\n1 4\n4 3\n3 2",
"output": "4"
},
{
"input": "1000000 9 11\n38 59 999999 199 283 4849 1000000 2 554\n39 94\n3 9\n1 39\n39 40\n40 292\n5399 5858\n292 49949\n49949 222\n222 38\n202 9494\n38 59",
"output": "38"
},
{
"input": "1000000 11 9\n19 28 39 82 99 929384 8298 892849 202020 777777 123123\n19 28\n28 39\n1 123124\n39 28\n28 99\n99 8298\n123124 123122\n2300 3200\n8298 1000000",
"output": "123122"
},
{
"input": "2 1 1\n1\n1 2",
"output": "1"
},
{
"input": "7 3 6\n1 4 5\n1 2\n2 3\n3 5\n4 5\n4 5\n4 5",
"output": "1"
},
{
"input": "10 3 8\n1 5 10\n1 2\n2 3\n3 4\n3 4\n3 4\n4 5\n5 6\n6 5",
"output": "1"
},
{
"input": "5 2 9\n2 4\n1 3\n3 5\n3 5\n3 4\n4 2\n2 4\n1 4\n1 2\n1 4",
"output": "4"
},
{
"input": "10 10 13\n1 2 3 4 5 6 7 8 9 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n6 7\n6 10\n10 9\n9 1\n1 10\n1 10\n1 10",
"output": "1"
},
{
"input": "3 3 3\n1 2 3\n1 2\n2 3\n3 2",
"output": "1"
},
{
"input": "100 7 7\n17 27 37 47 57 67 77\n49 39\n55 1\n50 3\n89 1\n1 99\n100 55\n98 55",
"output": "100"
},
{
"input": "9 1 9\n9\n1 2\n3 2\n4 3\n8 9\n4 5\n7 4\n8 5\n1 3\n3 2",
"output": "8"
},
{
"input": "300000 1 1\n200000\n300000 1",
"output": "300000"
},
{
"input": "203948 2 14\n203948 203946\n39 38\n4959 3030\n1 203947\n2929 9292\n203944 203948\n203947 203944\n203944 203922\n203922 203948\n2495 20495\n29419 5959\n12949 12\n49 29292\n1 94\n1 203",
"output": "203948"
},
{
"input": "203948 2 14\n203948 203947\n39 38\n4959 3030\n1 203947\n2929 9292\n203944 203948\n203947 203944\n203944 203922\n203922 203948\n2495 20495\n29419 5959\n12949 12\n49 29292\n1 94\n1 203",
"output": "203947"
},
{
"input": "100 2 5\n1 2\n2 39\n39 29\n99 100\n1 2\n2 39",
"output": "1"
},
{
"input": "3 1 1\n1\n1 2",
"output": "1"
},
{
"input": "5 2 2\n1 2\n1 2\n2 3",
"output": "1"
},
{
"input": "2 2 1\n1 2\n2 1",
"output": "1"
},
{
"input": "5 2 1\n1 2\n2 1",
"output": "1"
},
{
"input": "5 1 1\n5\n3 4",
"output": "1"
},
{
"input": "3 2 1\n1 2\n2 1",
"output": "1"
},
{
"input": "5 1 2\n2\n2 1\n2 3",
"output": "2"
},
{
"input": "3 1 2\n2\n2 1\n2 3",
"output": "2"
},
{
"input": "3 2 2\n2 3\n2 1\n2 3",
"output": "2"
},
{
"input": "4 2 1\n1 2\n2 1",
"output": "1"
},
{
"input": "4 1 1\n2\n2 3",
"output": "1"
},
{
"input": "3 2 1\n1 3\n3 1",
"output": "1"
},
{
"input": "10 1 3\n2\n2 1\n2 4\n9 10",
"output": "2"
},
{
"input": "5 2 4\n3 5\n1 2\n4 2\n3 4\n3 5",
"output": "3"
},
{
"input": "4 3 1\n1 2 3\n2 1",
"output": "1"
}
] | 93 | 0 | -1 | 1,822 |
|
465 | inc ARG | [
"implementation"
] | null | null | Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting of *n* bits. These bits are numbered from 1 to *n*. An integer is stored in the cell in the following way: the least significant bit is stored in the first bit of the cell, the next significant bit is stored in the second bit, and so on; the most significant bit is stored in the *n*-th bit.
Now Sergey wants to test the following instruction: "add 1 to the value of the cell". As a result of the instruction, the integer that is written in the cell must be increased by one; if some of the most significant bits of the resulting number do not fit into the cell, they must be discarded.
Sergey wrote certain values ββof the bits in the cell and is going to add one to its value. How many bits of the cell will change after the operation? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of bits in the cell.
The second line contains a string consisting of *n* characters β the initial state of the cell. The first character denotes the state of the first bit of the cell. The second character denotes the second least significant bit and so on. The last character denotes the state of the most significant bit. | Print a single integer β the number of bits in the cell which change their state after we add 1 to the cell. | [
"4\n1100\n",
"4\n1111\n"
] | [
"3\n",
"4\n"
] | In the first sample the cell ends up with value 0010, in the second sample β with 0000. | [
{
"input": "4\n1100",
"output": "3"
},
{
"input": "4\n1111",
"output": "4"
},
{
"input": "1\n0",
"output": "1"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "2\n00",
"output": "1"
},
{
"input": "2\n01",
"output": "1"
},
{
"input": "2\n10",
"output": "2"
},
{
"input": "2\n11",
"output": "2"
},
{
"input": "10\n0000000000",
"output": "1"
},
{
"input": "20\n11111111110110001100",
"output": "11"
},
{
"input": "50\n01011110100010000001010000100001001101001101101011",
"output": "1"
},
{
"input": "60\n111111111101111111111111111111111111111111111111111111111111",
"output": "11"
},
{
"input": "60\n111111111111111111111111111111111111111111111111111111111111",
"output": "60"
},
{
"input": "66\n111111010010011001110011000111000100011110011001111110011111111101",
"output": "7"
},
{
"input": "90\n000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "1"
},
{
"input": "91\n1011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "2"
},
{
"input": "92\n11111111101011111011111111111111111111111011101111111111011111111111111101111111101011111101",
"output": "10"
},
{
"input": "100\n0001011110100011001100100010111001000001111101101001001001001011110100101101010000000110100101110010",
"output": "1"
},
{
"input": "100\n0111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "1"
},
{
"input": "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "1"
},
{
"input": "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "1"
},
{
"input": "100\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "100"
},
{
"input": "100\n1011001110001000011111110011000100001110010110111101110110011011011000010100110001111100000010110010",
"output": "2"
},
{
"input": "100\n1101111011001111111111110011110111101110111111111111111111111111111111011111111111110111111111111111",
"output": "3"
},
{
"input": "100\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "100"
},
{
"input": "100\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110",
"output": "100"
},
{
"input": "100\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111101",
"output": "99"
},
{
"input": "100\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000",
"output": "96"
},
{
"input": "100\n1111111111111111111111111111111110011000110110001010101100100100001000010010010010000100110000010101",
"output": "34"
}
] | 62 | 0 | 3 | 1,824 |
|
903 | Hungry Student Problem | [
"greedy",
"implementation"
] | null | null | Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken.
CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large one β 7 chunks. Ivan wants to eat exactly *x* chunks. Now he wonders whether he can buy exactly this amount of chicken.
Formally, Ivan wants to know if he can choose two non-negative integers *a* and *b* in such a way that *a* small portions and *b* large ones contain exactly *x* chunks.
Help Ivan to answer this question for several values of *x*! | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100) β the number of testcases.
The *i*-th of the following *n* lines contains one integer *x**i* (1<=β€<=*x**i*<=β€<=100) β the number of chicken chunks Ivan wants to eat. | Print *n* lines, in *i*-th line output YES if Ivan can buy exactly *x**i* chunks. Otherwise, print NO. | [
"2\n6\n5\n"
] | [
"YES\nNO\n"
] | In the first example Ivan can buy two small portions.
In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much. | [
{
"input": "2\n6\n5",
"output": "YES\nNO"
},
{
"input": "100\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n80\n81\n82\n83\n84\n85\n86\n87\n88\n89\n90\n91\n92\n93\n94\n95\n96\n97\n98\n99\n100",
"output": "NO\nNO\nYES\nNO\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES"
},
{
"input": "3\n6\n6\n6",
"output": "YES\nYES\nYES"
},
{
"input": "47\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",
"output": "NO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO"
},
{
"input": "3\n1\n52\n76",
"output": "NO\nYES\nYES"
},
{
"input": "87\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES"
},
{
"input": "3\n3\n2\n1",
"output": "YES\nNO\nNO"
},
{
"input": "100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES"
}
] | 46 | 0 | 0 | 1,825 |
|
325 | Stadium and Games | [
"binary search",
"math"
] | null | null | Daniel is organizing a football tournament. He has come up with the following tournament format:
1. In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. 1. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are *x* teams, there will be games), and the tournament ends.
For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games.
Daniel has already booked the stadium for *n* games. Help him to determine how many teams he should invite so that the tournament needs exactly *n* games. You should print all possible numbers of teams that will yield exactly *n* games in ascending order, or -1 if there are no such numbers. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=1018), the number of games that should be played.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | Print all possible numbers of invited teams in ascending order, one per line. If exactly *n* games cannot be played, output one number: -1. | [
"3\n",
"25\n",
"2\n"
] | [
"3\n4\n",
"20\n",
"-1\n"
] | none | [
{
"input": "3",
"output": "3\n4"
},
{
"input": "25",
"output": "20"
},
{
"input": "2",
"output": "-1"
},
{
"input": "1",
"output": "2"
},
{
"input": "15",
"output": "10\n16"
},
{
"input": "314",
"output": "-1"
},
{
"input": "524800",
"output": "1025"
},
{
"input": "5149487579894806",
"output": "-1"
},
{
"input": "249999998807430810",
"output": "1414213558"
},
{
"input": "1000000000000000000",
"output": "-1"
},
{
"input": "4",
"output": "-1"
},
{
"input": "5",
"output": "-1"
},
{
"input": "6",
"output": "6"
},
{
"input": "7",
"output": "8"
},
{
"input": "8",
"output": "-1"
},
{
"input": "9",
"output": "-1"
},
{
"input": "10",
"output": "5"
},
{
"input": "11",
"output": "-1"
},
{
"input": "12",
"output": "12"
},
{
"input": "13",
"output": "-1"
},
{
"input": "14",
"output": "-1"
},
{
"input": "21",
"output": "7"
},
{
"input": "28",
"output": "14"
},
{
"input": "36",
"output": "9"
},
{
"input": "45",
"output": "18\n40"
},
{
"input": "55",
"output": "11"
},
{
"input": "78",
"output": "13"
},
{
"input": "105",
"output": "15"
},
{
"input": "120",
"output": "30"
},
{
"input": "136",
"output": "17"
},
{
"input": "171",
"output": "19\n144"
},
{
"input": "210",
"output": "21\n120"
},
{
"input": "255",
"output": "136\n256"
},
{
"input": "5460",
"output": "105\n1456"
},
{
"input": "16383",
"output": "8256\n16384"
},
{
"input": "391170",
"output": "885\n98176"
},
{
"input": "1906128",
"output": "1953\n121024"
},
{
"input": "576460752303423487",
"output": "576460752303423488"
},
{
"input": "499999999500000000",
"output": "1999999998"
},
{
"input": "250000001635857933",
"output": "2828427124"
},
{
"input": "999999998765257141",
"output": "2828427122"
},
{
"input": "321730048",
"output": "-1"
},
{
"input": "499999500000",
"output": "1999998"
},
{
"input": "250000000221644371",
"output": "1414213562"
},
{
"input": "58819626242454945",
"output": "342985791"
},
{
"input": "672900920488237864",
"output": "-1"
},
{
"input": "994374468178120050",
"output": "1410230101"
},
{
"input": "999971062750901550",
"output": "1414193101"
},
{
"input": "999999912498231750",
"output": "1414213501"
},
{
"input": "999999943610929003",
"output": "1414213523"
},
{
"input": "999999995936830020",
"output": "2828427118"
},
{
"input": "999999998765257141",
"output": "2828427122"
},
{
"input": "999999997351043580",
"output": "1414213561"
},
{
"input": "496",
"output": "62"
},
{
"input": "3012278988753",
"output": "4908994"
},
{
"input": "20000000100000000",
"output": "200000001"
},
{
"input": "980000156100006216",
"output": "2800000222"
},
{
"input": "995460657326506216",
"output": "2822000222"
},
{
"input": "38927073",
"output": "35284"
},
{
"input": "30110278526854603",
"output": "981595076"
},
{
"input": "6882",
"output": "888"
},
{
"input": "20263965249",
"output": "1610472"
},
{
"input": "936612417",
"output": "-1"
},
{
"input": "529914",
"output": "8184"
},
{
"input": "514948626567892275",
"output": "16237416336"
},
{
"input": "514948642805308611",
"output": "32474832672"
},
{
"input": "1459321801",
"output": "-1"
},
{
"input": "16358075516553",
"output": "5856031744"
},
{
"input": "1337521996548297",
"output": "105920063488"
},
{
"input": "4877709674134636",
"output": "-1"
},
{
"input": "487738618277701671",
"output": "8090864197632"
},
{
"input": "487746708154228600",
"output": "-1"
},
{
"input": "520088094975",
"output": "-1"
},
{
"input": "32767",
"output": "32768"
},
{
"input": "131071",
"output": "131072"
},
{
"input": "1310755",
"output": "-1"
},
{
"input": "32775625",
"output": "32768000"
},
{
"input": "57819024375",
"output": "52756480000"
},
{
"input": "1570397049375",
"output": "1059717120000"
},
{
"input": "72315871219375",
"output": "21203517440000"
},
{
"input": "5323259016854625",
"output": "212034912256000"
},
{
"input": "257957076",
"output": "257949696"
},
{
"input": "5180726200",
"output": "5179965440"
},
{
"input": "8355443183554431",
"output": "3355443233554432"
},
{
"input": "58687091686870911",
"output": "53687091736870912"
},
{
"input": "5000000250000000",
"output": "-1"
},
{
"input": "500000003500000003",
"output": "4000000004"
},
{
"input": "178120883702871",
"output": "178120883699712"
},
{
"input": "266081813928931",
"output": "266081813921792"
},
{
"input": "9005000239863810",
"output": "9005000231485440"
},
{
"input": "10475010",
"output": "2096640"
},
{
"input": "943414054006932870",
"output": "943413961980641280"
},
{
"input": "431105316312401832",
"output": "431105315111436288"
},
{
"input": "686288770539583120",
"output": "686288769778712576"
},
{
"input": "434351073512812035",
"output": "434351073436631040"
},
{
"input": "305752193461383075",
"output": "305752193451950080"
},
{
"input": "660058820389234315",
"output": "660058820386488320"
},
{
"input": "838795430598031275",
"output": "838795430597754880"
},
{
"input": "270215977642229850",
"output": "270215977642229760"
},
{
"input": "576460752303423490",
"output": "-1"
},
{
"input": "864691128455135232",
"output": "864691128455135232"
},
{
"input": "402653184",
"output": "402653184"
},
{
"input": "576460752303423487",
"output": "576460752303423488"
},
{
"input": "268435455",
"output": "134225920\n268435456"
},
{
"input": "530516448",
"output": "130284\n16418304"
},
{
"input": "8539349952",
"output": "522732\n132779008"
},
{
"input": "4095",
"output": "91\n2080\n4096"
},
{
"input": "7791518261859",
"output": "31580232\n1812942290944"
},
{
"input": "72057594037927935",
"output": "36028797153181696\n72057594037927936"
},
{
"input": "288230376151711743",
"output": "144115188344291328\n288230376151711744"
},
{
"input": "999999999999999999",
"output": "-1"
},
{
"input": "4095",
"output": "91\n2080\n4096"
},
{
"input": "500000002500000003",
"output": "1000000003"
},
{
"input": "605000000550000000",
"output": "1100000001"
},
{
"input": "1099511627775",
"output": "549756338176\n1099511627776"
},
{
"input": "73687091368435455",
"output": "53687091468435456"
},
{
"input": "965211250482432409",
"output": "-1"
},
{
"input": "432345564227567616",
"output": "432345564227567616"
},
{
"input": "138485688541650132",
"output": "138485688541642752"
},
{
"input": "4979826519",
"output": "2368241664"
},
{
"input": "1125899906842623",
"output": "562949970198528\n1125899906842624"
},
{
"input": "1073741823",
"output": "536887296\n1073741824"
},
{
"input": "36028797018963967",
"output": "36028797018963968"
}
] | 1,000 | 1,843,200 | 0 | 1,826 |
|
1,000 | Light It Up | [
"greedy"
] | null | null | Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment $0$ and turn power off at moment $M$. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.
The lamp allows only good programs. Good program can be represented as a non-empty array $a$, where $0 < a_1 < a_2 < \dots < a_{|a|} < M$. All $a_i$ must be integers. Of course, preinstalled program is a good program.
The lamp follows program $a$ in next manner: at moment $0$ turns power and light on. Then at moment $a_i$ the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment $1$ and then do nothing, the total time when the lamp is lit will be $1$. Finally, at moment $M$ the lamp is turning its power off regardless of its state.
Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program $a$, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $a$, or even at the begining or at the end of $a$.
Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from $x$ till moment $y$, then its lit for $y - x$ units of time. Segments of time when the lamp is lit are summed up. | First line contains two space separated integers $n$ and $M$ ($1 \le n \le 10^5$, $2 \le M \le 10^9$) β the length of program $a$ and the moment when power turns off.
Second line contains $n$ space separated integers $a_1, a_2, \dots, a_n$ ($0 < a_1 < a_2 < \dots < a_n < M$) β initially installed program $a$. | Print the only integer β maximum possible total time when the lamp is lit. | [
"3 10\n4 6 7\n",
"2 12\n1 10\n",
"2 7\n3 4\n"
] | [
"8\n",
"9\n",
"6\n"
] | In the first example, one of possible optimal solutions is to insert value $x = 3$ before $a_1$, so program will be $[3, 4, 6, 7]$ and time of lamp being lit equals $(3 - 0) + (6 - 4) + (10 - 7) = 8$. Other possible solution is to insert $x = 5$ in appropriate place.
In the second example, there is only one optimal solution: to insert $x = 2$ between $a_1$ and $a_2$. Program will become $[1, 2, 10]$, and answer will be $(1 - 0) + (10 - 2) = 9$.
In the third example, optimal answer is to leave program untouched, so answer will be $(3 - 0) + (7 - 4) = 6$. | [
{
"input": "3 10\n4 6 7",
"output": "8"
},
{
"input": "2 12\n1 10",
"output": "9"
},
{
"input": "2 7\n3 4",
"output": "6"
},
{
"input": "1 2\n1",
"output": "1"
},
{
"input": "5 10\n1 3 5 6 8",
"output": "6"
},
{
"input": "7 1000000000\n1 10001 10011 20011 20021 40021 40031",
"output": "999999969"
},
{
"input": "7 1000000000\n3 10001 10011 20011 20021 40021 40031",
"output": "999999969"
},
{
"input": "1 10\n1",
"output": "9"
},
{
"input": "1 10000000\n1",
"output": "9999999"
},
{
"input": "1 8\n1",
"output": "7"
},
{
"input": "7 17\n1 5 9 10 11 14 16",
"output": "9"
},
{
"input": "4 17\n1 5 9 10",
"output": "12"
},
{
"input": "5 12\n1 2 3 4 5",
"output": "9"
},
{
"input": "2 1000000000\n100 111",
"output": "999999989"
},
{
"input": "3 90591\n90579 90580 90581",
"output": "90589"
},
{
"input": "5 16\n1 2 3 4 5",
"output": "13"
},
{
"input": "6 981231233\n1 6 15 30 130 1875",
"output": "981229468"
},
{
"input": "1 3\n1",
"output": "2"
},
{
"input": "3 12\n4 7 11",
"output": "8"
},
{
"input": "1 100000\n1",
"output": "99999"
},
{
"input": "2 5\n1 3",
"output": "3"
},
{
"input": "1 1000000000\n1",
"output": "999999999"
},
{
"input": "1 1000000000\n999999999",
"output": "999999999"
},
{
"input": "3 4\n1 2 3",
"output": "2"
},
{
"input": "3 5\n1 2 3",
"output": "3"
},
{
"input": "1 4\n2",
"output": "3"
},
{
"input": "3 5\n1 3 4",
"output": "3"
},
{
"input": "9 20\n5 9 11 12 14 15 16 17 19",
"output": "12"
},
{
"input": "2 4\n1 2",
"output": "3"
},
{
"input": "1 10\n2",
"output": "9"
},
{
"input": "2 6\n2 4",
"output": "4"
},
{
"input": "2 4\n1 3",
"output": "2"
},
{
"input": "3 6\n1 2 4",
"output": "4"
},
{
"input": "7 9863\n65 96 97 98 101 112 1115",
"output": "9819"
}
] | 77 | 14,028,800 | 3 | 1,838 |
|
725 | Hidden Word | [
"brute force",
"constructive algorithms",
"implementation",
"strings"
] | null | null | Letβs define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
Youβre given a string *s* which consists of 27 upper-case English letters. Each English letter occurs at least once in *s*. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string *s*. If thereβs no solution, print "Impossible" (without the quotes). | The only line of the input contains the string *s*, consisting of 27 upper-case English letters. Each English letter occurs at least once in *s*. | Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible". | [
"ABCDEFGHIJKLMNOPQRSGTUVWXYZ\n",
"BUVTYZFQSNRIWOXXGJLKACPEMDH\n"
] | [
"YXWVUTGHIJKLM\nZABCDEFSRQPON\n",
"Impossible\n"
] | none | [
{
"input": "ABCDEFGHIJKLMNOPQRSGTUVWXYZ",
"output": "YXWVUTGHIJKLM\nZABCDEFSRQPON"
},
{
"input": "BUVTYZFQSNRIWOXXGJLKACPEMDH",
"output": "Impossible"
},
{
"input": "DYCEUXXKMGZOINVPHWQSRTABLJF",
"output": "Impossible"
},
{
"input": "UTEDBZRVWLOFUASHCYIPXGJMKNQ",
"output": "PIYCHSAUTEDBZ\nXGJMKNQFOLWVR"
},
{
"input": "ZWMFLTCQIAJEVUPODMSGXKHRNYB",
"output": "HKXGSMFLTCQIA\nRNYBZWDOPUVEJ"
},
{
"input": "QGZEMFKWLUHOVSXJTCPIYREDNAB",
"output": "ANDEMFKWLUHOV\nBQGZRYIPCTJXS"
},
{
"input": "BMVFGRNDOWTILZVHAKCQSXYEJUP",
"output": "XSQCKAHVFGRND\nYEJUPBMZLITWO"
},
{
"input": "MKNTKOBFLJSXWQPVUERDHIACYGZ",
"output": "VPQWXSJLFBOKN\nUERDHIACYGZMT"
},
{
"input": "YOFJVQSWBUZENPCXGQTHMDKAILR",
"output": "IAKDMHTQSWBUZ\nLRYOFJVGXCPNE"
},
{
"input": "GYCUAXSBNAWFIJPDQVETKZOMLHR",
"output": "TEVQDPJIFWAXS\nKZOMLHRGYCUNB"
},
{
"input": "BITCRJOKMPDDUSWAYXHQZEVGLFN",
"output": "Impossible"
},
{
"input": "XCDSLTYWJIGUBPHNFZWVMQARKOE",
"output": "OKRAQMVWJIGUB\nEXCDSLTYZFNHP"
},
{
"input": "XTSHBGLRJAMDUIPCWYOZVERNKQF",
"output": "XFQKNRJAMDUIP\nTSHBGLEVZOYWC"
},
{
"input": "RFKNZXHAIMVBWEBPTCSYOLJGDQU",
"output": "QDGJLOYSCTPBW\nURFKNZXHAIMVE"
},
{
"input": "HVDEBKMJTLKQORNWCZSGXYIPUAF",
"output": "XGSZCWNROQKMJ\nYIPUAFHVDEBLT"
},
{
"input": "XZTMCRBONHFIUVPKWSDLJQGAHYE",
"output": "TZXEYHFIUVPKW\nMCRBONAGQJLDS"
},
{
"input": "YAMVOHUJLEDCWZLXNRGPIQTBSKF",
"output": "SBTQIPGRNXLED\nKFYAMVOHUJZWC"
},
{
"input": "XECPFJBHINOWVLAGTUMRZYHQSDK",
"output": "XKDSQHINOWVLA\nECPFJBYZRMUTG"
},
{
"input": "UULGRBAODZENVCSMJTHXPWYKFIQ",
"output": "Impossible"
},
{
"input": "BADSLHIYGMZJQKTCOPRVUXFWENN",
"output": "Impossible"
},
{
"input": "TEGXHBUVZDPAMIJFQYCWRKSTNLO",
"output": "NTEGXHBUVZDPA\nLOSKRWCYQFJIM"
},
{
"input": "XQVBTCNIRFPLOHAYZUMKWEJSXDG",
"output": "DXQVBTCNIRFPL\nGSJEWKMUZYAHO"
},
{
"input": "MIDLBEUAGTNPYKFWHVSRJOXCZMQ",
"output": "MIDLBEUAGTNPY\nQZCXOJRSVHWFK"
},
{
"input": "NMGIFDZKBCVRYLTWOASXHEUQPJN",
"output": "NMGIFDZKBCVRY\nJPQUEHXSAOWTL"
},
{
"input": "AHGZCRJTKPMQUNBWSIYLDXEHFVO",
"output": "VFHGZCRJTKPMQ\nOAEXDLYISWBNU"
},
{
"input": "UNGHFQRCIPBZTEOAYJXLDMSKNWV",
"output": "WNGHFQRCIPBZT\nVUKSMDLXJYAOE"
},
{
"input": "MKBGVNDJRAWUEHFSYLIZCOPTXKQ",
"output": "QKBGVNDJRAWUE\nMXTPOCZILYSFH"
},
{
"input": "UTGDEJHCBKRWLYFSONAQVMPIXZT",
"output": "TGDEJHCBKRWLY\nUZXIPMVQANOSF"
},
{
"input": "BETRFOVLPCMWKHAXSGUDQYJTZIN",
"output": "IZTRFOVLPCMWK\nNBEJYQDUGSXAH"
},
{
"input": "HIDCLZUTPOQGEXFASJNYBVRMDKW",
"output": "WKDCLZUTPOQGE\nHIMRVBYNJSAFX"
},
{
"input": "CNHIKJWRLPXTQZVUGYDMBAOEFHS",
"output": "SHIKJWRLPXTQZ\nCNFEOABMDYGUV"
},
{
"input": "LCFNHUQWXBPOSJMYTGKDAZVREIF",
"output": "LFNHUQWXBPOSJ\nCIERVZADKGTYM"
},
{
"input": "OURNQJWMIXCLGSDVEKZAFBYNTPH",
"output": "HPTNQJWMIXCLG\nOURYBFAZKEVDS"
},
{
"input": "ZWFIRJNXVKHOUSTQBLEGYMAPIDC",
"output": "CDIRJNXVKHOUS\nZWFPAMYGELBQT"
},
{
"input": "UOWJXRKHZDNGLSAMEIYTQBVCFJP",
"output": "UPJXRKHZDNGLS\nOWFCVBQTYIEMA"
},
{
"input": "IHDTJLGRFUXQSOZEMVYKWCPANBT",
"output": "ITJLGRFUXQSOZ\nHDBNAPCWKYVME"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZA",
"output": "ABCDEFGHIJKLM\nZYXWVUTSRQPON"
},
{
"input": "ABACDEFGHIJKLMNOPQRSTUVWXYZ",
"output": "NMLKJIHGFEDCA\nOPQRSTUVWXYZB"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZG",
"output": "CBAGHIJKLMNOP\nDEFZYXWVUTSRQ"
},
{
"input": "ABCDEFGHGIJKLMNOPQRSTUVWXYZ",
"output": "TSRQPONMLKJIG\nUVWXYZABCDEFH"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZX",
"output": "KJIHGFEDCBAXY\nLMNOPQRSTUVWZ"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWYXYZ",
"output": "KJIHGFEDCBAZY\nLMNOPQRSTUVWX"
},
{
"input": "BUVTYZFQSNRIWOXGJLKACPEMDHB",
"output": "BUVTYZFQSNRIW\nHDMEPCAKLJGXO"
},
{
"input": "QWERTYUIOPASDFGHJKLZXCVBNMQ",
"output": "QWERTYUIOPASD\nMNBVCXZLKJHGF"
},
{
"input": "ABCBDEFGHIJKLMNOPQRSTUVWXYZ",
"output": "ONMLKJIHGFEDB\nPQRSTUVWXYZAC"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXAYZ",
"output": "YABCDEFGHIJKL\nZXWVUTSRQPONM"
},
{
"input": "ABCDEFGHIJKLMZYXWVUTSRQPONA",
"output": "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ"
},
{
"input": "BACDEFGHIJKLMNOPQRSTUVWXYZA",
"output": "ACDEFGHIJKLMN\nBZYXWVUTSRQPO"
}
] | 77 | 307,200 | 3 | 1,840 |
|
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"
}
] | 122 | 0 | -1 | 1,846 |
820 | Mister B and Angle in Polygon | [
"constructive algorithms",
"geometry",
"math"
] | null | null | On one quiet day all of sudden Mister B decided to draw angle *a* on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex *n*-gon (regular convex polygon with *n* sides).
That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices *v*1, *v*2, *v*3 such that the angle (where *v*2 is the vertex of the angle, and *v*1 and *v*3 lie on its sides) is as close as possible to *a*. In other words, the value should be minimum possible.
If there are many optimal solutions, Mister B should be satisfied with any of them. | First and only line contains two space-separated integers *n* and *a* (3<=β€<=*n*<=β€<=105, 1<=β€<=*a*<=β€<=180)Β β the number of vertices in the polygon and the needed angle, in degrees. | Print three space-separated integers: the vertices *v*1, *v*2, *v*3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to *n* in clockwise order. | [
"3 15\n",
"4 67\n",
"4 68\n"
] | [
"1 2 3\n",
"2 1 3\n",
"4 1 2\n"
] | In first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.
Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45β-β67|β<β|90β-β67|. Other correct answers are: "3 1 2", "3 2 4", "4 2 3", "4 3 1", "1 3 4", "1 4 2", "2 4 1", "4 1 3", "3 1 4", "3 4 2", "2 4 3", "2 3 1", "1 3 2", "1 2 4", "4 2 1".
In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90β-β68|β<β|45β-β68|. Other correct answers are: "2 1 4", "3 2 1", "1 2 3", "4 3 2", "2 3 4", "1 4 3", "3 4 1". | [
{
"input": "3 15",
"output": "2 1 3"
},
{
"input": "4 67",
"output": "2 1 3"
},
{
"input": "4 68",
"output": "2 1 4"
},
{
"input": "3 1",
"output": "2 1 3"
},
{
"input": "3 180",
"output": "2 1 3"
},
{
"input": "100000 1",
"output": "2 1 558"
},
{
"input": "100000 180",
"output": "2 1 100000"
},
{
"input": "100000 42",
"output": "2 1 23335"
},
{
"input": "100000 123",
"output": "2 1 68335"
},
{
"input": "5 1",
"output": "2 1 3"
},
{
"input": "5 36",
"output": "2 1 3"
},
{
"input": "5 54",
"output": "2 1 3"
},
{
"input": "5 55",
"output": "2 1 4"
},
{
"input": "5 70",
"output": "2 1 4"
},
{
"input": "5 89",
"output": "2 1 4"
},
{
"input": "5 90",
"output": "2 1 4"
},
{
"input": "5 91",
"output": "2 1 5"
},
{
"input": "5 111",
"output": "2 1 5"
},
{
"input": "5 126",
"output": "2 1 5"
},
{
"input": "5 127",
"output": "2 1 5"
},
{
"input": "5 141",
"output": "2 1 5"
},
{
"input": "5 162",
"output": "2 1 5"
},
{
"input": "5 180",
"output": "2 1 5"
},
{
"input": "6 46",
"output": "2 1 4"
},
{
"input": "6 33",
"output": "2 1 3"
},
{
"input": "13 4",
"output": "2 1 3"
},
{
"input": "23 11",
"output": "2 1 3"
},
{
"input": "11 119",
"output": "2 1 9"
},
{
"input": "13 117",
"output": "2 1 10"
},
{
"input": "18 174",
"output": "2 1 18"
},
{
"input": "8509 139",
"output": "2 1 6573"
},
{
"input": "29770 76",
"output": "2 1 12572"
},
{
"input": "59115 40",
"output": "2 1 13139"
},
{
"input": "68459 88",
"output": "2 1 33471"
},
{
"input": "85100 129",
"output": "2 1 60990"
},
{
"input": "100000 13",
"output": "2 1 7224"
},
{
"input": "100000 35",
"output": "2 1 19446"
},
{
"input": "100000 49",
"output": "2 1 27224"
},
{
"input": "100000 71",
"output": "2 1 39446"
},
{
"input": "100000 79",
"output": "2 1 43891"
},
{
"input": "100000 101",
"output": "2 1 56113"
},
{
"input": "100000 109",
"output": "2 1 60558"
},
{
"input": "100000 143",
"output": "2 1 79446"
},
{
"input": "100000 148",
"output": "2 1 82224"
},
{
"input": "100000 176",
"output": "2 1 97780"
},
{
"input": "4 16",
"output": "2 1 3"
}
] | 92 | 23,142,400 | 0 | 1,850 |
|
166 | Tetrahedron | [
"dp",
"math",
"matrices"
] | null | null | You are given a tetrahedron. Let's mark its vertices with letters *A*, *B*, *C* and *D* correspondingly.
An ant is standing in the vertex *D* of the tetrahedron. The ant is quite active and he wouldn't stay idle. At each moment of time he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can't stand on one place.
You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex *D* to itself in exactly *n* steps. In other words, you are asked to find out the number of different cyclic paths with the length of *n* from vertex *D* to itself. As the number can be quite large, you should print it modulo 1000000007 (109<=+<=7). | The first line contains the only integer *n* (1<=β€<=*n*<=β€<=107) β the required length of the cyclic path. | Print the only integer β the required number of ways modulo 1000000007 (109<=+<=7). | [
"2\n",
"4\n"
] | [
"3\n",
"21\n"
] | The required paths in the first sample are:
- *D*β-β*A*β-β*D* - *D*β-β*B*β-β*D* - *D*β-β*C*β-β*D* | [
{
"input": "2",
"output": "3"
},
{
"input": "4",
"output": "21"
},
{
"input": "1",
"output": "0"
},
{
"input": "3",
"output": "6"
},
{
"input": "5",
"output": "60"
},
{
"input": "6",
"output": "183"
},
{
"input": "7",
"output": "546"
},
{
"input": "8",
"output": "1641"
},
{
"input": "9",
"output": "4920"
},
{
"input": "10",
"output": "14763"
},
{
"input": "15",
"output": "3587226"
},
{
"input": "30",
"output": "782663359"
},
{
"input": "10000000",
"output": "192336614"
},
{
"input": "100",
"output": "721510432"
},
{
"input": "300",
"output": "327873818"
},
{
"input": "900",
"output": "295068084"
},
{
"input": "1500",
"output": "451187545"
},
{
"input": "3000",
"output": "645417275"
},
{
"input": "5000",
"output": "755610910"
},
{
"input": "10000",
"output": "723907367"
},
{
"input": "50000",
"output": "969527595"
},
{
"input": "90000",
"output": "548978368"
},
{
"input": "99999",
"output": "909741855"
},
{
"input": "100000",
"output": "729225554"
},
{
"input": "300000",
"output": "93822635"
},
{
"input": "800000",
"output": "178940616"
},
{
"input": "1000000",
"output": "266233856"
},
{
"input": "4000000",
"output": "882155933"
},
{
"input": "9000000",
"output": "295060537"
},
{
"input": "9999999",
"output": "730778875"
},
{
"input": "1000000",
"output": "266233856"
},
{
"input": "9999999",
"output": "730778875"
},
{
"input": "10000000",
"output": "192336614"
},
{
"input": "9999998",
"output": "576926295"
},
{
"input": "30",
"output": "782663359"
},
{
"input": "31",
"output": "347990060"
}
] | 124 | 0 | 0 | 1,856 |
|
181 | Series of Crimes | [
"brute force",
"geometry",
"implementation"
] | null | null | The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang.
The Berland capital's map is represented by an *n*<=Γ<=*m* rectangular table. Each cell of the table on the map represents some districts of the capital.
The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map.
Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed. | The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=100) β the number of rows and columns in the table, correspondingly.
Each of the next *n* lines contains *m* characters β the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".".
It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements. | Print two integers β the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right. | [
"3 2\n.*\n..\n**\n",
"3 3\n*.*\n*..\n...\n"
] | [
"1 1\n",
"2 3\n"
] | none | [
{
"input": "3 2\n.*\n..\n**",
"output": "1 1"
},
{
"input": "2 5\n*....\n*...*",
"output": "1 5"
},
{
"input": "7 2\n..\n**\n..\n..\n..\n..\n.*",
"output": "7 1"
},
{
"input": "7 2\n*.\n..\n..\n..\n..\n..\n**",
"output": "1 2"
},
{
"input": "2 10\n*......*..\n.......*..",
"output": "2 1"
},
{
"input": "10 3\n*..\n...\n...\n...\n...\n...\n...\n...\n...\n**.",
"output": "1 2"
},
{
"input": "100 2\n*.\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n**",
"output": "1 2"
},
{
"input": "99 3\n**.\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n*..",
"output": "99 2"
},
{
"input": "98 3\n...\n.*.\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n.**",
"output": "2 3"
},
{
"input": "99 3\n**.\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n*..",
"output": "99 2"
},
{
"input": "98 3\n...\n.*.\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n...\n.**",
"output": "2 3"
},
{
"input": "2 100\n...................................................................................................*\n*..................................................................................................*",
"output": "1 1"
},
{
"input": "2 2\n*.\n**",
"output": "1 2"
},
{
"input": "2 2\n**\n.*",
"output": "2 1"
},
{
"input": "2 3\n*.*\n..*",
"output": "2 1"
}
] | 92 | 0 | 3 | 1,861 |
|
803 | Maximal Binary Matrix | [
"constructive algorithms"
] | null | null | You are given matrix with *n* rows and *n* columns filled with zeroes. You should put *k* ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.
One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one.
If there exists no such matrix then output -1. | The first line consists of two numbers *n* and *k* (1<=β€<=*n*<=β€<=100, 0<=β€<=*k*<=β€<=106). | If the answer exists then output resulting matrix. Otherwise output -1. | [
"2 1\n",
"3 2\n",
"2 5\n"
] | [
"1 0 \n0 0 \n",
"1 0 0 \n0 1 0 \n0 0 0 \n",
"-1\n"
] | none | [
{
"input": "2 1",
"output": "1 0 \n0 0 "
},
{
"input": "3 2",
"output": "1 0 0 \n0 1 0 \n0 0 0 "
},
{
"input": "2 5",
"output": "-1"
},
{
"input": "1 0",
"output": "0 "
},
{
"input": "1 1",
"output": "1 "
},
{
"input": "20 398",
"output": "1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 \n1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 \n1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 \n1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 \n1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 \n1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 \n1 1 1 1..."
},
{
"input": "20 401",
"output": "-1"
},
{
"input": "100 3574",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "100 10000",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "100 10001",
"output": "-1"
},
{
"input": "2 3",
"output": "1 1 \n1 0 "
},
{
"input": "4 5",
"output": "1 1 1 0 \n1 0 0 0 \n1 0 0 0 \n0 0 0 0 "
},
{
"input": "5 6",
"output": "1 1 1 0 0 \n1 1 0 0 0 \n1 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 24",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 0 "
},
{
"input": "2 0",
"output": "0 0 \n0 0 "
},
{
"input": "3 5",
"output": "1 1 1 \n1 0 0 \n1 0 0 "
},
{
"input": "3 3",
"output": "1 1 0 \n1 0 0 \n0 0 0 "
},
{
"input": "5 10",
"output": "1 1 1 1 1 \n1 1 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 "
},
{
"input": "3 4",
"output": "1 1 0 \n1 1 0 \n0 0 0 "
},
{
"input": "4 3",
"output": "1 1 0 0 \n1 0 0 0 \n0 0 0 0 \n0 0 0 0 "
},
{
"input": "1 1000000",
"output": "-1"
},
{
"input": "3 6",
"output": "1 1 1 \n1 1 0 \n1 0 0 "
},
{
"input": "1 2",
"output": "-1"
},
{
"input": "1 0",
"output": "0 "
},
{
"input": "1 1",
"output": "1 "
},
{
"input": "1 2",
"output": "-1"
},
{
"input": "1 3",
"output": "-1"
},
{
"input": "1 4",
"output": "-1"
},
{
"input": "1 5",
"output": "-1"
},
{
"input": "1 6",
"output": "-1"
},
{
"input": "1 7",
"output": "-1"
},
{
"input": "1 8",
"output": "-1"
},
{
"input": "1 9",
"output": "-1"
},
{
"input": "1 10",
"output": "-1"
},
{
"input": "1 11",
"output": "-1"
},
{
"input": "1 12",
"output": "-1"
},
{
"input": "1 13",
"output": "-1"
},
{
"input": "1 14",
"output": "-1"
},
{
"input": "1 15",
"output": "-1"
},
{
"input": "1 16",
"output": "-1"
},
{
"input": "1 17",
"output": "-1"
},
{
"input": "1 18",
"output": "-1"
},
{
"input": "1 19",
"output": "-1"
},
{
"input": "1 20",
"output": "-1"
},
{
"input": "1 21",
"output": "-1"
},
{
"input": "1 22",
"output": "-1"
},
{
"input": "1 23",
"output": "-1"
},
{
"input": "1 24",
"output": "-1"
},
{
"input": "1 25",
"output": "-1"
},
{
"input": "1 26",
"output": "-1"
},
{
"input": "2 0",
"output": "0 0 \n0 0 "
},
{
"input": "2 1",
"output": "1 0 \n0 0 "
},
{
"input": "2 2",
"output": "1 0 \n0 1 "
},
{
"input": "2 3",
"output": "1 1 \n1 0 "
},
{
"input": "2 4",
"output": "1 1 \n1 1 "
},
{
"input": "2 5",
"output": "-1"
},
{
"input": "2 6",
"output": "-1"
},
{
"input": "2 7",
"output": "-1"
},
{
"input": "2 8",
"output": "-1"
},
{
"input": "2 9",
"output": "-1"
},
{
"input": "2 10",
"output": "-1"
},
{
"input": "2 11",
"output": "-1"
},
{
"input": "2 12",
"output": "-1"
},
{
"input": "2 13",
"output": "-1"
},
{
"input": "2 14",
"output": "-1"
},
{
"input": "2 15",
"output": "-1"
},
{
"input": "2 16",
"output": "-1"
},
{
"input": "2 17",
"output": "-1"
},
{
"input": "2 18",
"output": "-1"
},
{
"input": "2 19",
"output": "-1"
},
{
"input": "2 20",
"output": "-1"
},
{
"input": "2 21",
"output": "-1"
},
{
"input": "2 22",
"output": "-1"
},
{
"input": "2 23",
"output": "-1"
},
{
"input": "2 24",
"output": "-1"
},
{
"input": "2 25",
"output": "-1"
},
{
"input": "2 26",
"output": "-1"
},
{
"input": "3 0",
"output": "0 0 0 \n0 0 0 \n0 0 0 "
},
{
"input": "3 1",
"output": "1 0 0 \n0 0 0 \n0 0 0 "
},
{
"input": "3 2",
"output": "1 0 0 \n0 1 0 \n0 0 0 "
},
{
"input": "3 3",
"output": "1 1 0 \n1 0 0 \n0 0 0 "
},
{
"input": "3 4",
"output": "1 1 0 \n1 1 0 \n0 0 0 "
},
{
"input": "3 5",
"output": "1 1 1 \n1 0 0 \n1 0 0 "
},
{
"input": "3 6",
"output": "1 1 1 \n1 1 0 \n1 0 0 "
},
{
"input": "3 7",
"output": "1 1 1 \n1 1 0 \n1 0 1 "
},
{
"input": "3 8",
"output": "1 1 1 \n1 1 1 \n1 1 0 "
},
{
"input": "3 9",
"output": "1 1 1 \n1 1 1 \n1 1 1 "
},
{
"input": "3 10",
"output": "-1"
},
{
"input": "3 11",
"output": "-1"
},
{
"input": "3 12",
"output": "-1"
},
{
"input": "3 13",
"output": "-1"
},
{
"input": "3 14",
"output": "-1"
},
{
"input": "3 15",
"output": "-1"
},
{
"input": "3 16",
"output": "-1"
},
{
"input": "3 17",
"output": "-1"
},
{
"input": "3 18",
"output": "-1"
},
{
"input": "3 19",
"output": "-1"
},
{
"input": "3 20",
"output": "-1"
},
{
"input": "3 21",
"output": "-1"
},
{
"input": "3 22",
"output": "-1"
},
{
"input": "3 23",
"output": "-1"
},
{
"input": "3 24",
"output": "-1"
},
{
"input": "3 25",
"output": "-1"
},
{
"input": "3 26",
"output": "-1"
},
{
"input": "4 0",
"output": "0 0 0 0 \n0 0 0 0 \n0 0 0 0 \n0 0 0 0 "
},
{
"input": "4 1",
"output": "1 0 0 0 \n0 0 0 0 \n0 0 0 0 \n0 0 0 0 "
},
{
"input": "4 2",
"output": "1 0 0 0 \n0 1 0 0 \n0 0 0 0 \n0 0 0 0 "
},
{
"input": "4 3",
"output": "1 1 0 0 \n1 0 0 0 \n0 0 0 0 \n0 0 0 0 "
},
{
"input": "4 4",
"output": "1 1 0 0 \n1 1 0 0 \n0 0 0 0 \n0 0 0 0 "
},
{
"input": "4 5",
"output": "1 1 1 0 \n1 0 0 0 \n1 0 0 0 \n0 0 0 0 "
},
{
"input": "4 6",
"output": "1 1 1 0 \n1 1 0 0 \n1 0 0 0 \n0 0 0 0 "
},
{
"input": "4 7",
"output": "1 1 1 1 \n1 0 0 0 \n1 0 0 0 \n1 0 0 0 "
},
{
"input": "4 8",
"output": "1 1 1 1 \n1 1 0 0 \n1 0 0 0 \n1 0 0 0 "
},
{
"input": "4 9",
"output": "1 1 1 1 \n1 1 0 0 \n1 0 1 0 \n1 0 0 0 "
},
{
"input": "4 10",
"output": "1 1 1 1 \n1 1 1 0 \n1 1 0 0 \n1 0 0 0 "
},
{
"input": "4 11",
"output": "1 1 1 1 \n1 1 1 0 \n1 1 1 0 \n1 0 0 0 "
},
{
"input": "4 12",
"output": "1 1 1 1 \n1 1 1 1 \n1 1 0 0 \n1 1 0 0 "
},
{
"input": "4 13",
"output": "1 1 1 1 \n1 1 1 1 \n1 1 1 0 \n1 1 0 0 "
},
{
"input": "4 14",
"output": "1 1 1 1 \n1 1 1 1 \n1 1 1 0 \n1 1 0 1 "
},
{
"input": "4 15",
"output": "1 1 1 1 \n1 1 1 1 \n1 1 1 1 \n1 1 1 0 "
},
{
"input": "4 16",
"output": "1 1 1 1 \n1 1 1 1 \n1 1 1 1 \n1 1 1 1 "
},
{
"input": "4 17",
"output": "-1"
},
{
"input": "4 18",
"output": "-1"
},
{
"input": "4 19",
"output": "-1"
},
{
"input": "4 20",
"output": "-1"
},
{
"input": "4 21",
"output": "-1"
},
{
"input": "4 22",
"output": "-1"
},
{
"input": "4 23",
"output": "-1"
},
{
"input": "4 24",
"output": "-1"
},
{
"input": "4 25",
"output": "-1"
},
{
"input": "4 26",
"output": "-1"
},
{
"input": "5 0",
"output": "0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 1",
"output": "1 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 2",
"output": "1 0 0 0 0 \n0 1 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 3",
"output": "1 1 0 0 0 \n1 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 4",
"output": "1 1 0 0 0 \n1 1 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 5",
"output": "1 1 1 0 0 \n1 0 0 0 0 \n1 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 6",
"output": "1 1 1 0 0 \n1 1 0 0 0 \n1 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 7",
"output": "1 1 1 1 0 \n1 0 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 8",
"output": "1 1 1 1 0 \n1 1 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 \n0 0 0 0 0 "
},
{
"input": "5 9",
"output": "1 1 1 1 1 \n1 0 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 10",
"output": "1 1 1 1 1 \n1 1 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 11",
"output": "1 1 1 1 1 \n1 1 0 0 0 \n1 0 1 0 0 \n1 0 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 12",
"output": "1 1 1 1 1 \n1 1 1 0 0 \n1 1 0 0 0 \n1 0 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 13",
"output": "1 1 1 1 1 \n1 1 1 0 0 \n1 1 1 0 0 \n1 0 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 14",
"output": "1 1 1 1 1 \n1 1 1 1 0 \n1 1 0 0 0 \n1 1 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 15",
"output": "1 1 1 1 1 \n1 1 1 1 0 \n1 1 1 0 0 \n1 1 0 0 0 \n1 0 0 0 0 "
},
{
"input": "5 16",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 0 0 0 \n1 1 0 0 0 \n1 1 0 0 0 "
},
{
"input": "5 17",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 0 0 \n1 1 0 0 0 \n1 1 0 0 0 "
},
{
"input": "5 18",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 0 0 \n1 1 0 1 0 \n1 1 0 0 0 "
},
{
"input": "5 19",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 0 \n1 1 1 0 0 \n1 1 0 0 0 "
},
{
"input": "5 20",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 0 \n1 1 1 1 0 \n1 1 0 0 0 "
},
{
"input": "5 21",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 0 0 \n1 1 1 0 0 "
},
{
"input": "5 22",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 0 \n1 1 1 0 0 "
},
{
"input": "5 23",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 0 \n1 1 1 0 1 "
},
{
"input": "5 24",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 0 "
},
{
"input": "5 25",
"output": "1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 \n1 1 1 1 1 "
},
{
"input": "5 26",
"output": "-1"
},
{
"input": "100 0",
"output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0..."
},
{
"input": "100 1",
"output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0..."
},
{
"input": "100 2",
"output": "1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0..."
},
{
"input": "100 3",
"output": "1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0..."
},
{
"input": "100 99",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..."
},
{
"input": "100 100",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..."
},
{
"input": "100 101",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..."
},
{
"input": "100 9999",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "100 10000",
"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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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": "100 10001",
"output": "-1"
}
] | 46 | 0 | 3 | 1,862 |
|
900 | Position in Fraction | [
"math",
"number theory"
] | null | null | You have a fraction . You need to find the first occurrence of digit *c* into decimal notation of the fraction after decimal point. | The first contains three single positive integers *a*, *b*, *c* (1<=β€<=*a*<=<<=*b*<=β€<=105, 0<=β€<=*c*<=β€<=9). | Print position of the first occurrence of digit *c* into the fraction. Positions are numbered from 1 after decimal point. It there is no such position, print -1. | [
"1 2 0\n",
"2 3 7\n"
] | [
"2",
"-1"
] | The fraction in the first example has the following decimal notation: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/896357459a466614a0542f34c9cfb0cef1afc9ed.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The first zero stands on second position.
The fraction in the second example has the following decimal notation: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/130ba579a8276fc53a1917606eee9db58817f28d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There is no digit 7 in decimal notation of the fraction. | [
{
"input": "1 2 0",
"output": "2"
},
{
"input": "2 3 7",
"output": "-1"
},
{
"input": "1 100000 1",
"output": "5"
},
{
"input": "1 7 7",
"output": "6"
},
{
"input": "99999 100000 8",
"output": "-1"
},
{
"input": "44102 73848 2",
"output": "132"
},
{
"input": "7 31 3",
"output": "15"
},
{
"input": "8880 81608 9",
"output": "161"
},
{
"input": "4942 62768 5",
"output": "122"
},
{
"input": "69168 84860 4",
"output": "107"
},
{
"input": "971 1883 3",
"output": "130"
},
{
"input": "1636 3269 6",
"output": "150"
},
{
"input": "6873 7769 3",
"output": "163"
},
{
"input": "13805 15538 3",
"output": "164"
},
{
"input": "10958 21926 3",
"output": "117"
},
{
"input": "8 51 0",
"output": "10"
},
{
"input": "1 10 1",
"output": "1"
},
{
"input": "1 9 0",
"output": "-1"
},
{
"input": "53 101 6",
"output": "-1"
},
{
"input": "1 10001 9",
"output": "5"
},
{
"input": "25102 31579 2",
"output": "174"
},
{
"input": "38790 39359 0",
"output": "212"
},
{
"input": "47117 78718 0",
"output": "213"
},
{
"input": "1 57 0",
"output": "1"
},
{
"input": "1 3 0",
"output": "-1"
},
{
"input": "1 100 0",
"output": "1"
},
{
"input": "2 3 0",
"output": "-1"
},
{
"input": "99971 99989 0",
"output": "9"
},
{
"input": "567 1580 0",
"output": "6"
},
{
"input": "45 97 0",
"output": "39"
},
{
"input": "35 111 4",
"output": "-1"
},
{
"input": "1 2 5",
"output": "1"
},
{
"input": "1 7 0",
"output": "-1"
}
] | 62 | 5,529,600 | 0 | 1,864 |
|
134 | Pairs of Numbers | [
"brute force",
"dfs and similar",
"math",
"number theory"
] | null | null | Let's assume that we have a pair of numbers (*a*,<=*b*). We can get a new pair (*a*<=+<=*b*,<=*b*) or (*a*,<=*a*<=+<=*b*) from the given pair in a single step.
Let the initial pair of numbers be (1,1). Your task is to find number *k*, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals *n*. | The input contains the only integer *n* (1<=β€<=*n*<=β€<=106). | Print the only integer *k*. | [
"5\n",
"1\n"
] | [
"3\n",
"0\n"
] | The pair (1,1) can be transformed into a pair containing 5 in three moves: (1,1) βββ (1,2) βββ (3,2) βββ (5,2). | [
{
"input": "5",
"output": "3"
},
{
"input": "1",
"output": "0"
},
{
"input": "2",
"output": "1"
},
{
"input": "3",
"output": "2"
},
{
"input": "4",
"output": "3"
},
{
"input": "10",
"output": "5"
},
{
"input": "1009",
"output": "15"
},
{
"input": "2009",
"output": "17"
},
{
"input": "7009",
"output": "19"
},
{
"input": "9009",
"output": "20"
},
{
"input": "19009",
"output": "21"
},
{
"input": "29009",
"output": "22"
},
{
"input": "12434",
"output": "21"
},
{
"input": "342342",
"output": "28"
},
{
"input": "342235",
"output": "28"
},
{
"input": "362235",
"output": "28"
},
{
"input": "762235",
"output": "30"
},
{
"input": "878235",
"output": "30"
},
{
"input": "978235",
"output": "30"
},
{
"input": "1000000",
"output": "30"
},
{
"input": "6",
"output": "5"
},
{
"input": "10000",
"output": "20"
},
{
"input": "999999",
"output": "30"
},
{
"input": "524287",
"output": "29"
},
{
"input": "777777",
"output": "30"
},
{
"input": "123756",
"output": "26"
},
{
"input": "976438",
"output": "30"
},
{
"input": "434563",
"output": "28"
},
{
"input": "345634",
"output": "28"
},
{
"input": "65457",
"output": "24"
},
{
"input": "123456",
"output": "26"
},
{
"input": "999997",
"output": "30"
},
{
"input": "98989",
"output": "25"
},
{
"input": "8",
"output": "4"
},
{
"input": "123455",
"output": "26"
},
{
"input": "990001",
"output": "30"
},
{
"input": "123141",
"output": "26"
},
{
"input": "998",
"output": "16"
},
{
"input": "453422",
"output": "28"
},
{
"input": "623423",
"output": "29"
},
{
"input": "89",
"output": "9"
},
{
"input": "24234",
"output": "23"
},
{
"input": "999879",
"output": "30"
},
{
"input": "345612",
"output": "28"
},
{
"input": "998756",
"output": "30"
},
{
"input": "999989",
"output": "30"
},
{
"input": "999998",
"output": "30"
},
{
"input": "999912",
"output": "30"
},
{
"input": "100000",
"output": "25"
}
] | 171 | 614,400 | -1 | 1,871 |
|
914 | Palindromes in a Tree | [
"bitmasks",
"data structures",
"divide and conquer",
"trees"
] | null | null | You are given a tree (a connected acyclic undirected graph) of *n* vertices. Vertices are numbered from 1 to *n* and each vertex is assigned a character from a to t.
A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome.
For each vertex, output the number of palindromic paths passing through it.
Note: The path from vertex *u* to vertex *v* is considered to be the same as the path from vertex *v* to vertex *u*, and this path will be counted only once for each of the vertices it passes through. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=2Β·105) Β β the number of vertices in the tree.
The next *n*<=-<=1 lines each contain two integers *u* and *v* (1<=<=β€<=<=*u*,<=*v*<=<=β€<=<=*n*,<=*u*<=β <=*v*) denoting an edge connecting vertex *u* and vertex *v*. It is guaranteed that the given graph is a tree.
The next line contains a string consisting of *n* lowercase characters from a to t where the *i*-th (1<=β€<=*i*<=β€<=*n*) character is the label of vertex *i* in the tree. | Print *n* integers in a single line, the *i*-th of which is the number of palindromic paths passing through vertex *i* in the tree. | [
"5\n1 2\n2 3\n3 4\n3 5\nabcbb\n",
"7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs\n"
] | [
"1 3 4 3 3 \n",
"1 4 1 1 2 4 2 \n"
] | In the first sample case, the following paths are palindromic:
2β-β3β-β4
2β-β3β-β5
4β-β3β-β5
Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic:
1β-β2β-β3
1β-β2β-β3β-β4
1β-β2β-β3β-β5 | [
{
"input": "5\n1 2\n2 3\n3 4\n3 5\nabcbb",
"output": "1 3 4 3 3 "
},
{
"input": "7\n6 2\n4 3\n3 7\n5 2\n7 2\n1 4\nafefdfs",
"output": "1 4 1 1 2 4 2 "
},
{
"input": "5\n3 1\n3 5\n5 4\n5 2\nticdm",
"output": "1 1 1 1 1 "
},
{
"input": "10\n10 8\n3 2\n9 7\n1 5\n5 3\n7 6\n8 4\n10 9\n2 6\nqbilfkqcll",
"output": "1 1 1 2 1 1 2 3 4 5 "
},
{
"input": "20\n10 9\n15 14\n11 12\n2 3\n15 16\n2 1\n18 19\n20 19\n8 9\n7 6\n8 7\n12 13\n5 6\n4 3\n13 14\n18 17\n11 10\n16 17\n5 4\naabbccddeeffgghhiijj",
"output": "20 29 45 52 64 69 77 80 84 85 85 84 80 77 69 64 52 45 29 20 "
}
] | 30 | 0 | 0 | 1,873 |
|
5 | Follow Traffic Rules | [
"implementation",
"math"
] | D. Follow Traffic Rules | 1 | 64 | Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed.
It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of *a* km/h2, and has maximum speed of *v* km/h. The road has the length of *l* km, and the speed sign, limiting the speed to *w* km/h, is placed *d* km (1<=β€<=*d*<=<<=*l*) away from the capital of Berland. The car has a zero speed at the beginning of the journey. Find the minimum time that an average Berland citizen will need to get from the capital to Bercouver, if he drives at the optimal speed.
The car can enter Bercouver at any speed. | The first line of the input file contains two integer numbers *a* and *v* (1<=β€<=*a*,<=*v*<=β€<=10000). The second line contains three integer numbers *l*, *d* and *w* (2<=β€<=*l*<=β€<=10000; 1<=β€<=*d*<=<<=*l*; 1<=β€<=*w*<=β€<=10000). | Print the answer with at least five digits after the decimal point. | [
"1 1\n2 1 3\n",
"5 70\n200 170 40\n"
] | [
"2.500000000000\n",
"8.965874696353\n"
] | none | [
{
"input": "1 1\n2 1 3",
"output": "2.500000000000"
},
{
"input": "5 70\n200 170 40",
"output": "8.965874696353"
},
{
"input": "6 80\n100 50 10",
"output": "7.312347829731"
},
{
"input": "7 80\n100 50 50",
"output": "5.345224838248"
},
{
"input": "8 80\n100 50 199",
"output": "5.000000000000"
},
{
"input": "200 1000\n3 2 1",
"output": "0.290249882934"
},
{
"input": "200 1000\n3 2 10000",
"output": "0.173205080757"
},
{
"input": "200 1000\n1000 500 1023",
"output": "3.162277660168"
},
{
"input": "200 1000\n1000 999 10",
"output": "4.482261988326"
},
{
"input": "20 40\n10000 1 30",
"output": "251.000000000000"
},
{
"input": "20 40\n10000 799 30",
"output": "251.125000000000"
},
{
"input": "20 40\n9958 9799 30",
"output": "250.075000000000"
},
{
"input": "9998 9999\n3 2 1",
"output": "0.042231317453"
},
{
"input": "9998 9999\n3 2 6580",
"output": "0.024497347285"
},
{
"input": "9998 9999\n800 40 10000",
"output": "0.400040006001"
},
{
"input": "9998 9999\n800 516 124",
"output": "0.668565367679"
},
{
"input": "4 120\n5112 3000 130",
"output": "57.600000000000"
},
{
"input": "4 120\n5112 3000 113",
"output": "57.702083333333"
},
{
"input": "9000 1\n10000 9999 1",
"output": "10000.000055555556"
},
{
"input": "2 10000\n270 64 16",
"output": "16.431676725155"
},
{
"input": "2 20\n270 64 16",
"output": "18.500000000000"
},
{
"input": "2 16\n270 64 16",
"output": "20.875000000000"
},
{
"input": "2000 10000\n8000 4000 4000",
"output": "2.828427124746"
},
{
"input": "2000 4000\n8000 4000 4000",
"output": "3.000000000000"
},
{
"input": "2000 10\n8000 4000 4000",
"output": "800.002500000000"
},
{
"input": "7143 4847\n4193 2677 1991",
"output": "1.438097228927"
},
{
"input": "5744 5873\n3706 1656 8898",
"output": "1.142252435725"
},
{
"input": "7992 3250\n9987 6772 5806",
"output": "3.276251405251"
},
{
"input": "240 4275\n6270 1836 6361",
"output": "7.228416147400"
},
{
"input": "5369 9035\n1418 879 3344",
"output": "0.726785762909"
},
{
"input": "7062 9339\n2920 1289 8668",
"output": "0.909374070882"
},
{
"input": "8755 9643\n1193 27 3992",
"output": "0.522044043034"
},
{
"input": "448 3595\n2696 1020 5667",
"output": "3.469252698452"
},
{
"input": "2141 3899\n968 262 991",
"output": "0.967126013479"
},
{
"input": "3834 4202\n2471 607 6315",
"output": "1.136044961574"
},
{
"input": "5527 8154\n3974 3550 1639",
"output": "1.555031897139"
},
{
"input": "7220 8458\n2246 1326 6963",
"output": "0.788771617656"
},
{
"input": "8914 8762\n3749 1899 2287",
"output": "1.172208101814"
},
{
"input": "607 2714\n2021 1483 3963",
"output": "2.580499677039"
},
{
"input": "9788 8432\n2795 2025 3436",
"output": "0.863942827831"
},
{
"input": "26 12\n17 13 29",
"output": "1.647435897436"
},
{
"input": "12 42\n6 5 19",
"output": "1.000000000000"
},
{
"input": "50 22\n42 1 12",
"output": "2.129090909091"
},
{
"input": "38 3\n47 16 4",
"output": "15.706140350877"
},
{
"input": "24 33\n35 2 45",
"output": "1.748106060606"
},
{
"input": "11 13\n24 15 37",
"output": "2.437062937063"
},
{
"input": "49 43\n12 6 30",
"output": "0.699854212224"
},
{
"input": "35 23\n17 12 20",
"output": "1.078881987578"
},
{
"input": "23 4\n5 2 13",
"output": "1.336956521739"
},
{
"input": "8 28\n22 4 29",
"output": "2.345207879912"
},
{
"input": "38 35\n16 12 38",
"output": "0.917662935482"
},
{
"input": "21 41\n26 18 47",
"output": "1.573591584939"
},
{
"input": "2 50\n21 1 6",
"output": "4.582575694956"
},
{
"input": "32 7\n15 6 17",
"output": "2.252232142857"
},
{
"input": "15 13\n10 3 26",
"output": "1.202564102564"
},
{
"input": "46 20\n4 2 35",
"output": "0.417028828114"
},
{
"input": "26 28\n46 9 44",
"output": "2.181318681319"
},
{
"input": "9 35\n41 22 3",
"output": "4.577276992968"
},
{
"input": "39 42\n35 19 12",
"output": "1.803482716151"
},
{
"input": "28 24\n31 13 21",
"output": "1.733630952381"
}
] | 154 | 307,200 | 0 | 1,874 |
900 | Find Extra One | [
"geometry",
"implementation"
] | null | null | You have *n* distinct points on a plane, none of them lie on *OY* axis. Check that there is a point after removal of which the remaining points are located on one side of the *OY* axis. | The first line contains a single positive integer *n* (2<=β€<=*n*<=β€<=105).
The following *n* lines contain coordinates of the points. The *i*-th of these lines contains two single integers *x**i* and *y**i* (|*x**i*|,<=|*y**i*|<=β€<=109, *x**i*<=β <=0). No two points coincide. | Print "Yes" if there is such a point, "No" β otherwise.
You can print every letter in any case (upper or lower). | [
"3\n1 1\n-1 -1\n2 -1\n",
"4\n1 1\n2 2\n-1 1\n-2 2\n",
"3\n1 2\n2 1\n4 60\n"
] | [
"Yes",
"No",
"Yes"
] | In the first example the second point can be removed.
In the second example there is no suitable for the condition point.
In the third example any point can be removed. | [
{
"input": "3\n1 1\n-1 -1\n2 -1",
"output": "Yes"
},
{
"input": "4\n1 1\n2 2\n-1 1\n-2 2",
"output": "No"
},
{
"input": "3\n1 2\n2 1\n4 60",
"output": "Yes"
},
{
"input": "10\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n-1 -1",
"output": "Yes"
},
{
"input": "2\n1000000000 -1000000000\n1000000000 1000000000",
"output": "Yes"
},
{
"input": "23\n-1 1\n-1 2\n-2 4\n-7 -8\n-3 3\n-9 -14\n-5 3\n-6 2\n-7 11\n-4 4\n-8 5\n1 1\n-1 -1\n-1 -2\n-2 -4\n-7 8\n-3 -3\n-9 14\n-5 -3\n-6 -2\n-7 -11\n-4 -4\n-8 -5",
"output": "Yes"
},
{
"input": "4\n-1000000000 -1000000000\n1000000000 1000000000\n-1000000000 1000000000\n1000000000 -1000000000",
"output": "No"
},
{
"input": "2\n-1000000000 1000000000\n-1000000000 -1000000000",
"output": "Yes"
},
{
"input": "5\n-1 -1\n-2 2\n2 2\n2 -2\n3 2",
"output": "No"
},
{
"input": "2\n1 0\n-1 0",
"output": "Yes"
},
{
"input": "4\n-1 1\n-1 2\n-1 3\n-1 4",
"output": "Yes"
},
{
"input": "2\n-1 0\n1 0",
"output": "Yes"
},
{
"input": "2\n1 2\n-1 2",
"output": "Yes"
},
{
"input": "2\n8 0\n7 0",
"output": "Yes"
},
{
"input": "6\n-1 0\n-2 0\n-1 -1\n-1 5\n1 0\n1 1",
"output": "No"
},
{
"input": "4\n1 0\n2 0\n-1 0\n-2 0",
"output": "No"
},
{
"input": "4\n-2 0\n-1 0\n1 0\n2 0",
"output": "No"
},
{
"input": "2\n1 1\n-1 1",
"output": "Yes"
},
{
"input": "4\n-1 0\n-2 0\n1 0\n2 0",
"output": "No"
},
{
"input": "2\n4 3\n-4 -2",
"output": "Yes"
},
{
"input": "4\n1 0\n2 0\n-1 1\n-1 2",
"output": "No"
},
{
"input": "5\n1 1\n2 1\n3 1\n-1 1\n-2 1",
"output": "No"
},
{
"input": "2\n1 1\n-1 -1",
"output": "Yes"
},
{
"input": "4\n1 2\n1 0\n1 -2\n-1 2",
"output": "Yes"
},
{
"input": "5\n-2 3\n-3 3\n4 2\n3 2\n1 2",
"output": "No"
},
{
"input": "3\n2 0\n3 0\n4 0",
"output": "Yes"
},
{
"input": "5\n-3 1\n-2 1\n-1 1\n1 1\n2 1",
"output": "No"
},
{
"input": "4\n-3 0\n1 0\n2 0\n3 0",
"output": "Yes"
},
{
"input": "2\n1 0\n-1 1",
"output": "Yes"
},
{
"input": "3\n-1 0\n1 0\n2 0",
"output": "Yes"
},
{
"input": "5\n1 0\n3 0\n-1 0\n-6 0\n-4 1",
"output": "No"
},
{
"input": "5\n-1 2\n-2 2\n-3 1\n1 2\n2 3",
"output": "No"
},
{
"input": "3\n1 0\n-1 0\n-2 0",
"output": "Yes"
},
{
"input": "4\n1 0\n2 0\n3 1\n4 1",
"output": "Yes"
},
{
"input": "4\n1 0\n1 2\n1 3\n-1 5",
"output": "Yes"
},
{
"input": "4\n2 2\n2 5\n-2 3\n-2 0",
"output": "No"
},
{
"input": "4\n1 1\n-1 1\n-1 0\n-1 -1",
"output": "Yes"
},
{
"input": "4\n2 0\n3 0\n-3 -3\n-3 -4",
"output": "No"
},
{
"input": "4\n-1 0\n-2 0\n-3 0\n-4 0",
"output": "Yes"
},
{
"input": "2\n-1 1\n1 1",
"output": "Yes"
},
{
"input": "5\n1 1\n2 2\n3 3\n-4 -4\n-5 -5",
"output": "No"
},
{
"input": "5\n2 0\n3 0\n4 0\n5 0\n6 0",
"output": "Yes"
},
{
"input": "2\n-1 2\n1 2",
"output": "Yes"
},
{
"input": "4\n1 1\n2 1\n-3 0\n-4 0",
"output": "No"
},
{
"input": "4\n-1 0\n-2 0\n3 0\n4 0",
"output": "No"
},
{
"input": "3\n3 0\n2 0\n1 0",
"output": "Yes"
},
{
"input": "4\n-2 0\n-3 0\n1 -1\n3 1",
"output": "No"
},
{
"input": "3\n-1 -1\n1 1\n2 2",
"output": "Yes"
},
{
"input": "4\n-2 0\n-1 0\n2 0\n1 0",
"output": "No"
},
{
"input": "2\n-3 5\n3 5",
"output": "Yes"
},
{
"input": "2\n-1 5\n1 5",
"output": "Yes"
},
{
"input": "4\n2 0\n3 0\n-2 0\n-3 0",
"output": "No"
},
{
"input": "3\n-1 1\n1 1\n1 -1",
"output": "Yes"
},
{
"input": "2\n1 0\n2 0",
"output": "Yes"
},
{
"input": "4\n-1 1\n-2 1\n2 -1\n3 -1",
"output": "No"
},
{
"input": "5\n1 0\n2 0\n3 0\n-1 0\n-2 0",
"output": "No"
},
{
"input": "4\n-3 0\n-4 0\n-5 0\n-6 0",
"output": "Yes"
},
{
"input": "6\n-3 0\n-2 0\n-1 0\n1 0\n2 0\n3 0",
"output": "No"
},
{
"input": "4\n5 0\n5 1\n6 0\n6 1",
"output": "Yes"
}
] | 296 | 0 | 3 | 1,875 |
|
478 | Initial Bet | [
"implementation"
] | null | null | There are five people playing a game called "Generosity". Each person gives some non-zero number of coins *b* as an initial bet. After all players make their bets of *b* coins, the following operation is repeated for several times: a coin is passed from one player to some other player.
Your task is to write a program that can, given the number of coins each player has at the end of the game, determine the size *b* of the initial bet or find out that such outcome of the game cannot be obtained for any positive number of coins *b* in the initial bet. | The input consists of a single line containing five integers *c*1,<=*c*2,<=*c*3,<=*c*4 and *c*5 β the number of coins that the first, second, third, fourth and fifth players respectively have at the end of the game (0<=β€<=*c*1,<=*c*2,<=*c*3,<=*c*4,<=*c*5<=β€<=100). | Print the only line containing a single positive integer *b* β the number of coins in the initial bet of each player. If there is no such value of *b*, then print the only value "-1" (quotes for clarity). | [
"2 5 4 0 4\n",
"4 5 9 2 1\n"
] | [
"3\n",
"-1\n"
] | In the first sample the following sequence of operations is possible:
1. One coin is passed from the fourth player to the second player; 1. One coin is passed from the fourth player to the fifth player; 1. One coin is passed from the first player to the third player; 1. One coin is passed from the fourth player to the second player. | [
{
"input": "2 5 4 0 4",
"output": "3"
},
{
"input": "4 5 9 2 1",
"output": "-1"
},
{
"input": "0 0 0 0 0",
"output": "-1"
},
{
"input": "1 2 1 2 3",
"output": "-1"
},
{
"input": "100 0 0 0 0",
"output": "20"
},
{
"input": "2 3 4 5 6",
"output": "4"
},
{
"input": "1 1 1 1 1",
"output": "1"
},
{
"input": "0 1 2 3 4",
"output": "2"
},
{
"input": "100 100 100 100 100",
"output": "100"
},
{
"input": "93 100 99 90 98",
"output": "96"
},
{
"input": "99 99 99 99 99",
"output": "99"
},
{
"input": "99 98 98 99 100",
"output": "-1"
},
{
"input": "43 83 1 0 23",
"output": "30"
},
{
"input": "43 83 1 100 23",
"output": "50"
},
{
"input": "57 83 11 4 93",
"output": "-1"
},
{
"input": "87 38 19 33 100",
"output": "-1"
},
{
"input": "56 0 0 0 4",
"output": "12"
},
{
"input": "0 0 0 0 1",
"output": "-1"
},
{
"input": "99 100 100 100 100",
"output": "-1"
}
] | 46 | 0 | 0 | 1,877 |
|
277 | Learning Languages | [
"dfs and similar",
"dsu"
] | null | null | The "BerCorp" company has got *n* employees. These employees can use *m* approved official languages for the formal correspondence. The languages are numbered with integers from 1 to *m*. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are willing to learn any number of official languages, as long as the company pays their lessons. A study course in one language for one employee costs 1 berdollar.
Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their correspondence can be indirect, i. e. other employees can help out translating). | The first line contains two integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=100) β the number of employees and the number of languages.
Then *n* lines follow β each employee's language list. At the beginning of the *i*-th line is integer *k**i* (0<=β€<=*k**i*<=β€<=*m*) β the number of languages the *i*-th employee knows. Next, the *i*-th line contains *k**i* integers β *a**ij* (1<=β€<=*a**ij*<=β€<=*m*) β the identifiers of languages the *i*-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.
The numbers in the lines are separated by single spaces. | Print a single integer β the minimum amount of money to pay so that in the end every employee could write a letter to every other one (other employees can help out translating). | [
"5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5\n",
"8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1\n",
"2 2\n1 2\n0\n"
] | [
"0\n",
"2\n",
"1\n"
] | In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.
In the third sample employee 2 must learn language 2. | [
{
"input": "5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5",
"output": "0"
},
{
"input": "8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1",
"output": "2"
},
{
"input": "2 2\n1 2\n0",
"output": "1"
},
{
"input": "2 2\n0\n0",
"output": "2"
},
{
"input": "5 5\n1 3\n0\n0\n2 4 1\n0",
"output": "4"
},
{
"input": "6 2\n0\n0\n2 1 2\n1 1\n1 1\n0",
"output": "3"
},
{
"input": "7 3\n3 1 3 2\n3 2 1 3\n2 2 3\n1 1\n2 2 3\n3 3 2 1\n3 2 3 1",
"output": "0"
},
{
"input": "8 4\n0\n0\n4 2 3 1 4\n4 2 1 4 3\n3 4 3 1\n1 2\n2 4 1\n2 4 2",
"output": "2"
},
{
"input": "10 10\n5 7 5 2 8 1\n7 10 6 9 5 8 2 4\n2 2 7\n5 8 6 9 10 1\n2 9 5\n3 6 5 2\n6 5 8 7 9 10 4\n0\n1 1\n2 8 6",
"output": "1"
},
{
"input": "11 42\n4 20 26 9 24\n14 34 7 28 32 12 15 26 4 10 38 21 20 8 11\n4 21 8 36 6\n11 32 1 39 11 21 10 25 17 26 15 4\n2 8 12\n2 21 31\n8 17 10 3 39 32 30 5 15\n20 24 20 38 17 4 7 21 19 32 28 31 22 30 37 10 5 33 2 13 9\n7 38 34 42 27 20 11 6\n3 40 3 39\n14 39 40 4 30 33 8 36 28 14 23 16 7 25 9",
"output": "0"
},
{
"input": "100 100\n1 33\n0\n2 35 4\n2 40 78\n1 69\n0\n1 2\n0\n0\n2 81 34\n1 45\n0\n1 87\n1 50\n1 97\n0\n2 100 59\n0\n0\n0\n1 45\n0\n0\n0\n1 6\n1 54\n0\n0\n0\n4 79 96 52 84\n1 86\n0\n0\n0\n0\n0\n0\n0\n0\n3 90 2 80\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n1 2\n0\n1 64\n0\n1 78\n1 82\n0\n0\n0\n0\n0\n1 6\n0\n0\n2 47 57\n1 95\n0\n2 91 79\n0\n1 27\n0\n1 74\n0\n0\n1 14\n0\n0\n2 90 19\n0\n1 10\n0\n0\n0\n0\n0\n1 57\n0\n2 28 50\n0\n0\n0\n1 47\n0\n0\n1 14\n0\n1 84\n1 1\n0\n0",
"output": "87"
},
{
"input": "2 2\n2 1 2\n2 1 2",
"output": "0"
},
{
"input": "2 2\n2 1 2\n1 1",
"output": "0"
},
{
"input": "2 2\n1 2\n1 1",
"output": "1"
},
{
"input": "3 100\n0\n0\n0",
"output": "3"
},
{
"input": "100 100\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0",
"output": "100"
},
{
"input": "3 3\n0\n0\n0",
"output": "3"
}
] | 280 | 0 | 0 | 1,878 |
|
73 | LionAge II | [
"dp"
] | C. LionAge II | 2 | 256 | Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character β non-empty string *s*, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than *k* letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters *x* and *y* (*x* immediately precedes *y*) the bonus *c*(*x*,<=*y*) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most *k* letters in the name of the Vasya's character. | The first line contains character's name *s* and an integer number *k* (0<=β€<=*k*<=β€<=100). The length of the nonempty string *s* does not exceed 100. The second line contains an integer number *n* (0<=β€<=*n*<=β€<=676) β amount of pairs of letters, giving bonus to the euphony. The next *n* lines contain description of these pairs Β«*x* *y* *c*Β», which means that sequence *xy* gives bonus *c* (*x*,<=*y* β lowercase Latin letters, <=-<=1000<=β€<=*c*<=β€<=1000). It is guaranteed that no pair *x* *y* mentioned twice in the input data. | Output the only number β maximum possible euphony ΠΎf the new character's name. | [
"winner 4\n4\ns e 7\no s 8\nl o 13\no o 8\n",
"abcdef 1\n5\na b -10\nb c 5\nc d 5\nd e 5\ne f 5\n"
] | [
"36",
"20"
] | In the first example the most euphony name will be *looser*. It is easy to calculate that its euphony is 36. | [
{
"input": "winner 4\n4\ns e 7\no s 8\nl o 13\no o 8",
"output": "36"
},
{
"input": "abcdef 1\n5\na b -10\nb c 5\nc d 5\nd e 5\ne f 5",
"output": "20"
},
{
"input": "akcbd 2\n3\na d 55\nb z 100\nb c 50",
"output": "155"
},
{
"input": "adcbd 1\n3\na d 55\nb z 100\nb c 50",
"output": "155"
},
{
"input": "abcbd 1\n3\na b 55\nb z 100\nb c 50",
"output": "205"
},
{
"input": "vswlx 1\n3\nz l 509\nb i 287\na o 952",
"output": "509"
},
{
"input": "srtlmx 2\n2\ne a -167\nc v -932",
"output": "0"
},
{
"input": "dlcmexn 3\n3\no k -42\no h 527\nf g -654",
"output": "527"
},
{
"input": "jmiqoyqf 4\n0",
"output": "0"
},
{
"input": "owhgcafpz 2\n40\nn n 951\nr n -857\ny o -228\nl c 369\nq g -735\nm g 723\nv y -445\ng z -853\nk f -549\ny h -591\ns h -918\nl p -899\ng t -849\nb y -29\nx l -555\ne x -435\nz w -780\nw k -267\ne n -453\nb f -338\nr y -146\ng b 544\nq q 720\nw c 817\nx n 797\nr m 134\nz a 847\nh o 208\nt s 362\nw t 316\nk u 475\nt k -180\nm w -441\nh n 495\nu p 984\nu j -267\no i 818\nh q -168\nl f -901\no x 434",
"output": "1802"
},
{
"input": "nkbfiidriqbiprwifmug 10\n23\nb l -137\nl p -307\no q -167\na u 166\np k -35\nk r -722\na d 363\nl u 580\nt p 1000\np i -883\nr r -698\nh o -773\ny j 992\np c -898\ng b 19\na m -629\nz k 857\na i 746\nz h -518\nh d 939\na s -332\nf p -544\np v -530",
"output": "7034"
},
{
"input": "xd 2\n0",
"output": "0"
},
{
"input": "glccn 2\n15\nd m -183\ny h -463\no z -453\ny p -280\no o -22\nu y -407\np a -999\na j -647\np w -245\ni b -94\nl u -149\nf r -934\nu m -564\nx n -145\nk d -586",
"output": "0"
},
{
"input": "pwlechvmtw 0\n36\ng g 742\nk b 372\nf g -860\nb k 48\nf a 845\nd k -305\na g 400\ng k 796\nd a -575\nb f -76\na f 912\nd f 339\na d 83\nk d 344\nd b 149\na a -3\na k -144\ng d -849\nf f 590\nd g 223\nb a 849\ng b 72\nk f 867\nb g 901\nk a 154\nf b 274\nb d -327\ng f 684\nd d 583\nk g -990\ng a -265\nf k 378\na b 58\nk k -117\nb b 19\nf d -887",
"output": "0"
},
{
"input": "xmxjoupuuu 2\n36\ng g 979\nb g 943\nb a 804\nk b -9\nk f -717\nk k 404\ng k -408\nf k -827\nb k 212\nf d 923\na k -12\nb d -646\nd k 7\ng f -324\ng a -573\nd b -374\nf g -233\nk d -485\nd a 649\na d 611\na b 66\nb f 24\nd g -769\nd f -484\nk a 207\nd d 397\nk g -350\ng b -487\nf a 428\nb b -80\na f -521\nf b -626\na g -787\ng d -740\na a 642\nf f -537",
"output": "979"
},
{
"input": "nyecwtjemqutvqq 15\n16\ng s 994\nf f 234\ng a 289\nf s -442\ns s -383\na s -636\na a 425\ns f 398\ns g 10\nf a -621\ng f 94\na g 923\ns a -344\ng g -108\na f 918\nf g 819",
"output": "10761"
},
{
"input": "emrvvhupytoxzhqxmuop 1\n49\ng g -558\nd g 845\nd k -745\nb g -773\nf f -733\nb s -491\na s -894\ng d -565\ns k -756\nb a 373\nb d 398\nk f 250\na b 531\nf g -27\nf b 125\na g -555\ns f 540\ng b 194\nk s -636\ns b -955\nd a -520\ng f -97\nf s -204\ns a 171\nb k -304\ns g -160\na a -567\na f -455\nd d -571\nd b 238\nf k -398\nk b -485\ns s -786\nb f -620\nb b 837\nk a -20\nk k 478\nf a -901\ns d -571\na k 321\na d 539\nf d 750\nk g -987\ng k -962\ng a -778\nk d 335\nd f -473\nd s -648\ng s -963",
"output": "0"
},
{
"input": "tipipjvztnlnmiiphovswquwqeuvopprwnx 22\n36\na a -841\nd g -832\nb k 263\nb a -161\ng f -796\nk g 324\nb f -738\nk d -3\nk k -72\nf d 932\nf b -893\nk b -979\nk a 451\nb b 416\nf f -53\ng a -125\ng g -621\nk f -628\na k 626\nf k 42\nb g -997\nd a -499\nd b -287\nd f 412\ng k 305\nf a -156\nd k -278\ng b -303\nd d 482\nb d 542\na g 391\na f 964\na b -189\nf g 707\ng d -46\na d -913",
"output": "14544"
},
{
"input": "chjorrmydvtvscyyjrguiepeurnlzmzxiekecpimsnojxyrvxq 0\n1\na a 255",
"output": "0"
},
{
"input": "nsdtaoqsmzmsndvnrkmyzdcmhdhuqrjnhygdkhquqleptykynlumfvqeprssihihpgodgdnksrwvtgnzkdopohnshjcnjdglwote 100\n1\nb b 999",
"output": "98901"
},
{
"input": "jqlmevbfblbworrurhdkktptnkvirnzlspzswuppdndtzmjdsnodzkkzbxuqzxqlkecozygumnwtfolzpkwctlhnpzvjknzmylhf 100\n1\na a -369",
"output": "0"
},
{
"input": "qgfqhkmidddhcmdnidqfsovwspmpgwnskeafdohshhdbpbfmmehuhhwpdachhinoqqphtijsejwxfbujfynanajrvoeayuxdqesn 10\n1\nz z 15",
"output": "135"
},
{
"input": "djeqhiwlsyjqdvdymfjjdypkswwwncjsqmurkvcisdsdvmuvrivpsxnyojjsgesfticndhghhqejcckgiwqjyverqqytlpkgcryp 1\n4\na z 260\nz z 329\na a -757\nz a 565",
"output": "0"
},
{
"input": "oamldkbphxyboqvnkghdwggtpgmszulowrvvjbfpnurstldrsriepgjrdaxfpdmtwemkdlsaodlhthdkroqasjnlen 80\n1\nd d 644",
"output": "56028"
}
] | 2,000 | 30,208,000 | 0 | 1,885 |
626 | Robot Sequence | [
"brute force",
"implementation"
] | null | null | Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of *n* commands, each either 'U', 'R', 'D', or 'L'Β β instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices. | The first line of the input contains a single positive integer, *n* (1<=β€<=*n*<=β€<=200)Β β the number of commands.
The next line contains *n* characters, each either 'U', 'R', 'D', or 'L'Β β Calvin's source code. | Print a single integerΒ β the number of contiguous substrings that Calvin can execute and return to his starting square. | [
"6\nURLLDR\n",
"4\nDLUU\n",
"7\nRLRLRLR\n"
] | [
"2\n",
"0\n",
"12\n"
] | In the first case, the entire source code works, as well as the "RL" substring in the second and third characters.
Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result. | [
{
"input": "6\nURLLDR",
"output": "2"
},
{
"input": "4\nDLUU",
"output": "0"
},
{
"input": "7\nRLRLRLR",
"output": "12"
},
{
"input": "1\nR",
"output": "0"
},
{
"input": "100\nURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDLURDL",
"output": "1225"
},
{
"input": "200\nLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR",
"output": "100"
},
{
"input": "20\nLDURLDURRLRUDLRRUDLU",
"output": "29"
},
{
"input": "140\nDLDLULULDRDDDLLUDRRDLLUULLDDLDLUURLDLDRDUDDLRRDURUUUUURLDUDDLLRRLLDRRRDDDDDUDUULLURRDLDULUDLLUUDRRLUDULUDUDULULUURURRDUURRDLULLURUDDDDRDRDRD",
"output": "125"
},
{
"input": "194\nULLLDLLDRUUDURRULLRLUUURDRLLURDUDDUDLULRLDRUDURLDLRDLLLLUDDRRRULULULUDDULRURURLLDLDLDRUDUUDULRULDDRRLRDRULLDRULLLLRRDDLLLLULDRLUULRUUULDUUDLDLDUUUDDLDDRULDRRLUURRULLDULRRDLLRDURDLUUDUDLLUDDULDDD",
"output": "282"
},
{
"input": "200\nDDDURLLUUULUDDURRDLLDDLLRLUULUULDDDLRRDLRRDUDURDUDRRLLDRDUDDLDDRDLURRRLLRDRRLLLRDDDRDRRLLRRLULRUULRLDLUDRRRDDUUURLLUDRLDUDRLLRLRRLUDLRULDUDDRRLLRLURDLRUDDDURLRDUDUUURLLULULRDRLDLDRURDDDLLRUDDRDUDDDLRU",
"output": "408"
},
{
"input": "197\nDUUDUDUDUDUUDUUDUUUDDDDUUUDUUUDUUUUUDUUUDDUDDDUUDUDDDUUDDUUUUUUUDUDDDDDUUUUUDDDDDDUUUUDDUDDUDDDUDUUUDUUDUDUDUUUDUDDDDUUDDUDDDDUDDDUDUUUDUUDUUUDDDDUUUDUUDDUUUUUDDDDUUDUUDDDDUDDUUDUUUDDDDUDUUUDDDUUDU",
"output": "1995"
},
{
"input": "200\nLLLLRLLRLLRRRRLLRRLRRLRRRLLLRRLRRRRLLRRLLRRRLRLRLRRLLRLLRRLLLRRRRLRLLRLLLRLLLRRLLLRLRLRRRRRRRLRRRLRLRLLLLRLRRRRRLRRLRLLLLRLLLRRLRRLLRLRLLLRRLLRRLRRRRRLRLRRLRLLRLLLLRLRRRLRRLRLLRLRRLRRRRRLRRLLLRRRRRLLR",
"output": "1368"
},
{
"input": "184\nUUUDDUDDDDDUDDDDUDDUUUUUDDDUUDDUDUUDUUUDDUDDDDDDDDDDUDUDDUUDDDUUDDUDUDDDUUDUDUUUUDDUDUUUDDUDUUUUDUUDDUUDUUUDUDUDDUDUDDDUUDDDDUUUUUDDDUDUDUDUDUDUUUDUDDUUDDUDUUDUDUUUDUUDDDDUDDDDUDUUDUUD",
"output": "1243"
},
{
"input": "187\nRLLRLRRLLRRLRRRRLLRLLRLLLLRRRLLLRLLLLRRLRLRRRRRRLLRRLRLLRRRLLRRLLLRRLRRLRLLLLRRRRLRRLLRRLRRRRLLLLRRLRLRLRRRRRLLRLRLRLRLRLRLLLRLLLLLRRRLLRLRRRLLLRRLLLLLRLLRLLLRRRLLLRRLRRRLLLRRLRLLRRLRLRLR",
"output": "1501"
},
{
"input": "190\nUULLLUUULLLULLUULUUUUULUUULLULLULUULLUULLUUULULUULLUULLUUULULLLLLLULLLLLULUULLULLULLLUULUULLLUUUULLLLUUULLUUULLLULULUULULLUULULULUUULLUUUULLUUULULUULLLLULLLLLUULLUULULLULUUUUUULULLLULLUULUUU",
"output": "0"
},
{
"input": "46\nULUURRRRLDRDRDDDURRRLLLDDULLRRRRRLUDDLRDRULLLL",
"output": "23"
},
{
"input": "70\nUUDRLDRDRUDLLURURULRDULRRDULDUDDRUULLDDDDDRLLRDURRDULRDLRUUUDDLRUURRLD",
"output": "86"
},
{
"input": "198\nURLLUDRDUUDRDLLRURULLRRLRRUULRLULUUDRRURLRUURRDRUUDRLRURLLULRDDDDDRDDRRRLRUDULLDDLLLUDRLDRUDRDLDUULLUUUULULLRLDDRDURDRURLULDRURLLDDULURULDLUUUUULDLURRLLDLULLDULRUURRLDLLUUURDLDDUDUULRLUDULLULDRDRLRL",
"output": "160"
},
{
"input": "22\nDUDDDURURUDURRUDRDULUL",
"output": "10"
},
{
"input": "200\nUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUDUD",
"output": "10000"
},
{
"input": "4\nRRDR",
"output": "0"
},
{
"input": "6\nUULLLL",
"output": "0"
},
{
"input": "2\nDU",
"output": "1"
},
{
"input": "6\nUURRRR",
"output": "0"
},
{
"input": "101\nRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
"output": "0"
}
] | 77 | 0 | 3 | 1,887 |
|
602 | Approximating a Constant Range | [
"dp",
"implementation",
"two pointers"
] | null | null | When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challengingΒ β but why not make a similar programming contest problem while we're at it?
You're given a sequence of *n* data points *a*1,<=...,<=*a**n*. There aren't any big jumps between consecutive data pointsΒ β for each 1<=β€<=*i*<=<<=*n*, it's guaranteed that |*a**i*<=+<=1<=-<=*a**i*|<=β€<=1.
A range [*l*,<=*r*] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let *M* be the maximum and *m* the minimum value of *a**i* for *l*<=β€<=*i*<=β€<=*r*; the range [*l*,<=*r*] is almost constant if *M*<=-<=*m*<=β€<=1.
Find the length of the longest almost constant range. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of data points.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=100<=000). | Print a single numberΒ β the maximum length of an almost constant range of the given sequence. | [
"5\n1 2 3 3 2\n",
"11\n5 4 5 5 6 7 8 8 8 7 6\n"
] | [
"4\n",
"5\n"
] | In the first sample, the longest almost constant range is [2,β5]; its length (the number of data points in it) is 4.
In the second sample, there are three almost constant ranges of length 4: [1,β4], [6,β9] and [7,β10]; the only almost constant range of the maximum length 5 is [6,β10]. | [
{
"input": "5\n1 2 3 3 2",
"output": "4"
},
{
"input": "11\n5 4 5 5 6 7 8 8 8 7 6",
"output": "5"
},
{
"input": "2\n3 2",
"output": "2"
},
{
"input": "4\n1001 1000 1000 1001",
"output": "4"
},
{
"input": "4\n1 1 2 3",
"output": "3"
},
{
"input": "3\n1 2 1",
"output": "3"
},
{
"input": "3\n1 2 3",
"output": "2"
},
{
"input": "18\n10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9",
"output": "3"
},
{
"input": "3\n1 2 2",
"output": "3"
},
{
"input": "4\n10 9 10 9",
"output": "4"
},
{
"input": "4\n4 3 2 3",
"output": "3"
},
{
"input": "4\n8 8 7 7",
"output": "4"
},
{
"input": "3\n99998 99999 100000",
"output": "2"
},
{
"input": "3\n100000 99999 99998",
"output": "2"
},
{
"input": "3\n1 1 1",
"output": "3"
},
{
"input": "2\n99999 100000",
"output": "2"
},
{
"input": "2\n100000 100000",
"output": "2"
},
{
"input": "2\n1 1",
"output": "2"
},
{
"input": "15\n1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000",
"output": "15"
}
] | 249 | 7,372,800 | 3 | 1,888 |
|
396 | On Changing Tree | [
"data structures",
"graphs",
"trees"
] | null | null | You are given a rooted tree consisting of *n* vertices numbered from 1 to *n*. The root of the tree is a vertex number 1.
Initially all vertices contain number 0. Then come *q* queries, each query has one of the two types:
- The format of the query: 1 *v* *x* *k*. In response to the query, you need to add to the number at vertex *v* number *x*; to the numbers at the descendants of vertex *v* at distance 1, add *x*<=-<=*k*; and so on, to the numbers written in the descendants of vertex *v* at distance *i*, you need to add *x*<=-<=(*i*Β·*k*). The distance between two vertices is the number of edges in the shortest path between these vertices. - The format of the query: 2 *v*. In reply to the query you should print the number written in vertex *v* modulo 1000000007 (109<=+<=7).
Process the queries given in the input. | The first line contains integer *n* (1<=β€<=*n*<=β€<=3Β·105) βΒ the number of vertices in the tree. The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=... *p**n* (1<=β€<=*p**i*<=<<=*i*), where *p**i* is the number of the vertex that is the parent of vertex *i* in the tree.
The third line contains integer *q* (1<=β€<=*q*<=β€<=3Β·105) β the number of queries. Next *q* lines contain the queries, one per line. The first number in the line is *type*. It represents the type of the query. If *type*<==<=1, then next follow space-separated integers *v*,<=*x*,<=*k* (1<=β€<=*v*<=β€<=*n*; 0<=β€<=*x*<=<<=109<=+<=7; 0<=β€<=*k*<=<<=109<=+<=7). If *type*<==<=2, then next follows integer *v* (1<=β€<=*v*<=β€<=*n*) βΒ the vertex where you need to find the value of the number. | For each query of the second type print on a single line the number written in the vertex from the query. Print the number modulo 1000000007 (109<=+<=7). | [
"3\n1 1\n3\n1 1 2 1\n2 1\n2 2\n"
] | [
"2\n1\n"
] | You can read about a rooted tree here: http://en.wikipedia.org/wiki/Tree_(graph_theory). | [
{
"input": "3\n1 1\n3\n1 1 2 1\n2 1\n2 2",
"output": "2\n1"
},
{
"input": "10\n1 2 3 4 4 3 3 6 7\n10\n1 6 13 98\n1 7 17 66\n1 5 32 39\n1 1 9 5\n1 7 27 11\n1 1 24 79\n1 5 87 86\n2 2\n1 5 9 38\n2 5",
"output": "999999956\n999999832"
},
{
"input": "1\n\n1\n2 1",
"output": "0"
}
] | 30 | 0 | 0 | 1,890 |
|
884 | Boxes And Balls | [
"data structures",
"greedy"
] | null | null | Ivan has *n* different boxes. The first of them contains some balls of *n* different colors.
Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every *i* (1<=β€<=*i*<=β€<=*n*) *i*-th box will contain all balls with color *i*.
In order to do this, Ivan will make some turns. Each turn he does the following:
1. Ivan chooses any non-empty box and takes all balls from this box; 1. Then Ivan chooses any *k* empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into *k* non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either *k*<==<=2 or *k*<==<=3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.
Help Ivan to determine the minimum possible penalty of the game! | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=200000) β the number of boxes and colors.
The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109), where *a**i* is the number of balls with color *i*. | Print one number β the minimum possible penalty of the game. | [
"3\n1 2 3\n",
"4\n2 3 4 5\n"
] | [
"6\n",
"19\n"
] | In the first example you take all the balls from the first box, choose *k*β=β3 and sort all colors to corresponding boxes. Penalty is 6.
In the second example you make two turns:
1. Take all the balls from the first box, choose *k*β=β3, put balls of color 3 to the third box, of color 4 β to the fourth box and the rest put back into the first box. Penalty is 14; 1. Take all the balls from the first box, choose *k*β=β2, put balls of color 1 to the first box, of color 2 β to the second box. Penalty is 5.
Total penalty is 19. | [
{
"input": "3\n1 2 3",
"output": "6"
},
{
"input": "4\n2 3 4 5",
"output": "19"
},
{
"input": "6\n1 4 4 4 4 4",
"output": "38"
},
{
"input": "8\n821407370 380061316 428719552 90851747 825473738 704702117 845629927 245820158",
"output": "8176373828"
},
{
"input": "1\n10",
"output": "0"
},
{
"input": "1\n4",
"output": "0"
},
{
"input": "1\n12312",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n3 4",
"output": "7"
}
] | 93 | 3,072,000 | -1 | 1,891 |
|
946 | Partition | [
"greedy"
] | null | null | You are given a sequence *a* consisting of *n* integers. You may partition this sequence into two sequences *b* and *c* in such a way that every element belongs exactly to one of these sequences.
Let *B* be the sum of elements belonging to *b*, and *C* be the sum of elements belonging to *c* (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of *B*<=-<=*C*? | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100) β the number of elements in *a*.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (<=-<=100<=β€<=*a**i*<=β€<=100) β the elements of sequence *a*. | Print the maximum possible value of *B*<=-<=*C*, where *B* is the sum of elements of sequence *b*, and *C* is the sum of elements of sequence *c*. | [
"3\n1 -2 0\n",
"6\n16 23 16 15 42 8\n"
] | [
"3\n",
"120\n"
] | In the first example we may choose *b*β=β{1,β0}, *c*β=β{β-β2}. Then *B*β=β1, *C*β=ββ-β2, *B*β-β*C*β=β3.
In the second example we choose *b*β=β{16,β23,β16,β15,β42,β8}, *c*β=β{} (an empty sequence). Then *B*β=β120, *C*β=β0, *B*β-β*C*β=β120. | [
{
"input": "3\n1 -2 0",
"output": "3"
},
{
"input": "6\n16 23 16 15 42 8",
"output": "120"
},
{
"input": "1\n-1",
"output": "1"
},
{
"input": "100\n-100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 -100",
"output": "10000"
},
{
"input": "2\n-1 5",
"output": "6"
},
{
"input": "3\n-2 0 1",
"output": "3"
},
{
"input": "12\n-1 -2 -3 4 4 -6 -6 56 3 3 -3 3",
"output": "94"
},
{
"input": "4\n1 -1 1 -1",
"output": "4"
},
{
"input": "4\n100 -100 100 -100",
"output": "400"
},
{
"input": "3\n-2 -5 10",
"output": "17"
},
{
"input": "5\n1 -2 3 -4 5",
"output": "15"
},
{
"input": "3\n-100 100 -100",
"output": "300"
},
{
"input": "6\n1 -1 1 -1 1 -1",
"output": "6"
},
{
"input": "6\n2 -2 2 -2 2 -2",
"output": "12"
},
{
"input": "9\n12 93 -2 0 0 0 3 -3 -9",
"output": "122"
},
{
"input": "6\n-1 2 4 -5 -3 55",
"output": "70"
},
{
"input": "6\n-12 8 68 -53 1 -15",
"output": "157"
},
{
"input": "2\n-2 1",
"output": "3"
},
{
"input": "3\n100 -100 100",
"output": "300"
},
{
"input": "5\n100 100 -1 -100 2",
"output": "303"
},
{
"input": "6\n-5 -4 -3 -2 -1 0",
"output": "15"
},
{
"input": "6\n4 4 4 -3 -3 2",
"output": "20"
},
{
"input": "2\n-1 2",
"output": "3"
},
{
"input": "1\n100",
"output": "100"
},
{
"input": "5\n-1 -2 3 1 2",
"output": "9"
},
{
"input": "5\n100 -100 100 -100 100",
"output": "500"
},
{
"input": "5\n1 -1 1 -1 1",
"output": "5"
},
{
"input": "4\n0 0 0 -1",
"output": "1"
},
{
"input": "5\n100 -100 -1 2 100",
"output": "303"
},
{
"input": "2\n75 0",
"output": "75"
},
{
"input": "4\n55 56 -59 -58",
"output": "228"
},
{
"input": "2\n9 71",
"output": "80"
},
{
"input": "2\n9 70",
"output": "79"
},
{
"input": "2\n9 69",
"output": "78"
},
{
"input": "2\n100 -100",
"output": "200"
},
{
"input": "4\n-9 4 -9 5",
"output": "27"
},
{
"input": "42\n91 -27 -79 -56 80 -93 -23 10 80 94 61 -89 -64 81 34 99 31 -32 -69 92 79 -9 73 66 -8 64 99 99 58 -19 -40 21 1 -33 93 -23 -62 27 55 41 57 36",
"output": "2348"
},
{
"input": "7\n-1 2 2 2 -1 2 -1",
"output": "11"
},
{
"input": "6\n-12 8 17 -69 7 -88",
"output": "201"
},
{
"input": "3\n1 -2 5",
"output": "8"
},
{
"input": "6\n-2 3 -4 5 6 -1",
"output": "21"
},
{
"input": "2\n-5 1",
"output": "6"
},
{
"input": "4\n2 2 -2 4",
"output": "10"
},
{
"input": "68\n21 47 -75 -25 64 83 83 -21 89 24 43 44 -35 34 -42 92 -96 -52 -66 64 14 -87 25 -61 -78 83 -96 -18 95 83 -93 -28 75 49 87 65 -93 -69 -2 95 -24 -36 -61 -71 88 -53 -93 -51 -81 -65 -53 -46 -56 6 65 58 19 100 57 61 -53 44 -58 48 -8 80 -88 72",
"output": "3991"
},
{
"input": "5\n5 5 -10 -1 1",
"output": "22"
},
{
"input": "3\n-1 2 3",
"output": "6"
},
{
"input": "76\n57 -38 -48 -81 93 -32 96 55 -44 2 38 -46 42 64 71 -73 95 31 -39 -62 -1 75 -17 57 28 52 12 -11 82 -84 59 -86 73 -97 34 97 -57 -85 -6 39 -5 -54 95 24 -44 35 -18 9 91 7 -22 -61 -80 54 -40 74 -90 15 -97 66 -52 -49 -24 65 21 -93 -29 -24 -4 -1 76 -93 7 -55 -53 1",
"output": "3787"
},
{
"input": "5\n-1 -2 1 2 3",
"output": "9"
},
{
"input": "4\n2 2 -2 -2",
"output": "8"
},
{
"input": "6\n100 -100 100 -100 100 -100",
"output": "600"
},
{
"input": "100\n-59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 61 -9 95 42 -73 -64 91 -96 2 53 -8 82 -79 16 18 -5 -53 26 71 38 -31 12 -33 -1 -65 -6 3 -89 22 33 -27 -36 41 11 -47 -32 47 -56 -38 57 -63 -41 23 41 29 78 16 -65 90 -58 -12 6 -60 42 -36 -52 -54 -95 -10 29 70 50 -94 1 93 48 -71 -77 -16 54 56 -60 66 76 31 8 44 -61 -74 23 37 38 18 -18 29 41",
"output": "4362"
},
{
"input": "2\n-1 1",
"output": "2"
},
{
"input": "3\n1 -2 100",
"output": "103"
},
{
"input": "5\n1 -2 3 1 2",
"output": "9"
},
{
"input": "10\n100 -10 -100 10 10 10 10 10 10 10",
"output": "280"
},
{
"input": "4\n2 0 -2 4",
"output": "8"
},
{
"input": "4\n3 -3 1 -1",
"output": "8"
},
{
"input": "3\n1 -1 1",
"output": "3"
},
{
"input": "4\n2 5 -2 4",
"output": "13"
},
{
"input": "2\n-2 2",
"output": "4"
},
{
"input": "3\n1 -2 1",
"output": "4"
},
{
"input": "5\n-1 -2 1 1 -1",
"output": "6"
},
{
"input": "4\n-2 0 2 4",
"output": "8"
},
{
"input": "8\n-42 7 87 -16 -5 65 -88 1",
"output": "311"
},
{
"input": "3\n1 -3 4",
"output": "8"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "2\n0 1",
"output": "1"
},
{
"input": "3\n-1 2 -1",
"output": "4"
},
{
"input": "18\n-21 12 65 66 -24 62 82 35 -45 -47 28 37 5 -32 22 -14 -69 -95",
"output": "761"
},
{
"input": "4\n-1 1 -1 1",
"output": "4"
},
{
"input": "5\n-1 2 1 1 1",
"output": "6"
},
{
"input": "3\n1 1 1",
"output": "3"
}
] | 140 | 0 | 3 | 1,893 |
|
121 | Lucky Sum | [
"implementation"
] | 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.
Let *next*(*x*) be the minimum lucky number which is larger than or equals *x*. Petya is interested what is the value of the expression *next*(*l*)<=+<=*next*(*l*<=+<=1)<=+<=...<=+<=*next*(*r*<=-<=1)<=+<=*next*(*r*). Help him solve this problem. | The single line contains two integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=109) β the left and right interval limits. | In the single line print the only number β the sum *next*(*l*)<=+<=*next*(*l*<=+<=1)<=+<=...<=+<=*next*(*r*<=-<=1)<=+<=*next*(*r*).
Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"2 7\n",
"7 7\n"
] | [
"33\n",
"7\n"
] | In the first sample: *next*(2)β+β*next*(3)β+β*next*(4)β+β*next*(5)β+β*next*(6)β+β*next*(7)β=β4β+β4β+β4β+β7β+β7β+β7β=β33
In the second sample: *next*(7)β=β7 | [
{
"input": "2 7",
"output": "33"
},
{
"input": "7 7",
"output": "7"
},
{
"input": "1 9",
"output": "125"
},
{
"input": "4 7",
"output": "25"
},
{
"input": "12 47",
"output": "1593"
},
{
"input": "6 77",
"output": "4012"
},
{
"input": "1 100",
"output": "14247"
},
{
"input": "1000000000 1000000000",
"output": "4444444444"
},
{
"input": "77 77",
"output": "77"
},
{
"input": "69 788",
"output": "452195"
},
{
"input": "474 747",
"output": "202794"
},
{
"input": "4 77777",
"output": "4070145675"
},
{
"input": "1 1000000",
"output": "1394675359387"
},
{
"input": "47 744447",
"output": "381286992761"
},
{
"input": "47444 1000000000",
"output": "1394672348253941136"
},
{
"input": "48 854888",
"output": "749733199853"
},
{
"input": "854444 985555",
"output": "582719941728"
},
{
"input": "774744 774747",
"output": "3098985"
},
{
"input": "654 987654",
"output": "1339803940266"
},
{
"input": "477777 1000000000",
"output": "1394672167300009765"
},
{
"input": "77777 777777777",
"output": "407018021649898097"
},
{
"input": "963 85555574",
"output": "7526978888069560"
},
{
"input": "47 7444",
"output": "38125896"
},
{
"input": "1 1000000000",
"output": "1394672350065645019"
},
{
"input": "474 999888555",
"output": "1394177038954402791"
},
{
"input": "777 1000000000",
"output": "1394672350065239125"
},
{
"input": "7 777777774",
"output": "407018023386632646"
},
{
"input": "369 852",
"output": "606732"
},
{
"input": "47 47",
"output": "47"
},
{
"input": "747 748",
"output": "1521"
},
{
"input": "77777440 77777444",
"output": "388887220"
},
{
"input": "987545885 999875584",
"output": "54798666661186800"
},
{
"input": "2 777777776",
"output": "407018024942188226"
},
{
"input": "1 1",
"output": "4"
},
{
"input": "1 2",
"output": "8"
},
{
"input": "999999999 1000000000",
"output": "8888888888"
},
{
"input": "777777777 1000000000",
"output": "987654325123456789"
},
{
"input": "777777778 1000000000",
"output": "987654324345679012"
},
{
"input": "5 6",
"output": "14"
},
{
"input": "49 49",
"output": "74"
},
{
"input": "6 6",
"output": "7"
},
{
"input": "3 4",
"output": "8"
}
] | 46 | 307,200 | 0 | 1,895 |
|
485 | Factory | [
"implementation",
"math",
"matrices"
] | null | null | One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were *x* details in the factory storage, then by the end of the day the factory has to produce (remainder after dividing *x* by *m*) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory.
The board of directors are worried that the production by the given plan may eventually stop (that means that there will be Π° moment when the current number of details on the factory is divisible by *m*).
Given the number of details *a* on the first day and number *m* check if the production stops at some moment. | The first line contains two integers *a* and *m* (1<=β€<=*a*,<=*m*<=β€<=105). | Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No". | [
"1 5\n",
"3 6\n"
] | [
"No\n",
"Yes\n"
] | none | [
{
"input": "1 5",
"output": "No"
},
{
"input": "3 6",
"output": "Yes"
},
{
"input": "1 8",
"output": "Yes"
},
{
"input": "2 3",
"output": "No"
},
{
"input": "3 24",
"output": "Yes"
},
{
"input": "1 1",
"output": "Yes"
},
{
"input": "100000 100000",
"output": "Yes"
},
{
"input": "1 99989",
"output": "No"
},
{
"input": "512 2",
"output": "Yes"
},
{
"input": "100 24",
"output": "No"
},
{
"input": "1 100000",
"output": "No"
},
{
"input": "100000 1",
"output": "Yes"
},
{
"input": "3 99929",
"output": "No"
},
{
"input": "99961 99971",
"output": "No"
},
{
"input": "1 65536",
"output": "Yes"
},
{
"input": "4 65536",
"output": "Yes"
},
{
"input": "3 65536",
"output": "Yes"
},
{
"input": "32768 65536",
"output": "Yes"
},
{
"input": "65535 65536",
"output": "Yes"
},
{
"input": "1 65535",
"output": "No"
},
{
"input": "98812 100000",
"output": "No"
},
{
"input": "10 5",
"output": "Yes"
},
{
"input": "6 8",
"output": "Yes"
}
] | 93 | 0 | -1 | 1,896 |
|
821 | Okabe and Future Gadget Laboratory | [
"implementation"
] | null | null | Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an *n* by *n* square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every *x*,<=*y* such that 1<=β€<=*x*,<=*y*<=β€<=*n* and *a**x*,<=*y*<=β <=1, there should exist two indices *s* and *t* so that *a**x*,<=*y*<==<=*a**x*,<=*s*<=+<=*a**t*,<=*y*, where *a**i*,<=*j* denotes the integer in *i*-th row and *j*-th column.
Help Okabe determine whether a given lab is good! | The first line of input contains the integer *n* (1<=β€<=*n*<=β€<=50)Β β the size of the lab.
The next *n* lines contain *n* space-separated integers denoting a row of the grid. The *j*-th integer in the *i*-th row is *a**i*,<=*j* (1<=β€<=*a**i*,<=*j*<=β€<=105). | Print "Yes" if the given lab is good and "No" otherwise.
You can output each letter in upper or lower case. | [
"3\n1 1 2\n2 3 1\n6 4 1\n",
"3\n1 5 2\n1 1 1\n1 2 3\n"
] | [
"Yes\n",
"No\n"
] | In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".
In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | [
{
"input": "3\n1 1 2\n2 3 1\n6 4 1",
"output": "Yes"
},
{
"input": "3\n1 5 2\n1 1 1\n1 2 3",
"output": "No"
},
{
"input": "1\n1",
"output": "Yes"
},
{
"input": "4\n1 1 1 1\n1 11 1 2\n2 5 1 4\n3 9 4 1",
"output": "Yes"
},
{
"input": "4\n1 1 1 1\n1 7 1 1\n1 3 1 2\n2 6 3 1",
"output": "Yes"
},
{
"input": "4\n1 1 1 1\n1 12 1 2\n4 4 1 3\n5 10 6 1",
"output": "Yes"
},
{
"input": "4\n1 1 1 1\n1 13 1 2\n4 5 1 3\n5 11 6 1",
"output": "Yes"
},
{
"input": "4\n1 1 1 1\n1 13 1 2\n4 5 1 3\n7 11 6 1",
"output": "No"
},
{
"input": "4\n1 1 1 1\n1 14 1 6\n4 5 1 5\n3 8 2 1",
"output": "Yes"
},
{
"input": "10\n3 2 6 5 1 2 1 1 2 1\n1 2 2 3 1 4 1 1 1 1\n1 1 3 1 2 2 2 1 1 1\n1 3 1 2 1 2 1 1 1 1\n12 1 4 3 3 1 1 1 2 1\n8 1 1 1 1 3 6 5 1 3\n1 1 1 2 4 1 3 2 1 1\n1 1 1 1 1 1 1 2 1 1\n1 2 1 1 1 1 1 1 1 1\n2 1 1 1 1 1 1 1 1 2",
"output": "Yes"
},
{
"input": "4\n1 2 3 5\n1 1 1 1\n1 1 1 1\n1 1 1 1",
"output": "No"
},
{
"input": "1\n2",
"output": "No"
},
{
"input": "3\n1 1 4\n1 1 2\n1 1 2",
"output": "No"
},
{
"input": "1\n5",
"output": "No"
},
{
"input": "3\n1 2 1\n2 2 3\n1 3 1",
"output": "No"
},
{
"input": "3\n2 1 1\n2 1 1\n2 1 1",
"output": "No"
},
{
"input": "1\n60000",
"output": "No"
}
] | 62 | 409,600 | 3 | 1,901 |
|
817 | Really Big Numbers | [
"binary search",
"brute force",
"dp",
"math"
] | null | null | Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number *x* is really big if the difference between *x* and the sum of its digits (in decimal representation) is not less than *s*. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are β in fact, he needs to calculate the quantity of really big numbers that are not greater than *n*.
Ivan tried to do the calculations himself, but soon realized that it's too difficult for him. So he asked you to help him in calculations. | The first (and the only) line contains two integers *n* and *s* (1<=β€<=*n*,<=*s*<=β€<=1018). | Print one integer β the quantity of really big numbers that are not greater than *n*. | [
"12 1\n",
"25 20\n",
"10 9\n"
] | [
"3\n",
"0\n",
"1\n"
] | In the first example numbers 10, 11 and 12 are really big.
In the second example there are no really big numbers that are not greater than 25 (in fact, the first really big number is 30: 30β-β3ββ₯β20).
In the third example 10 is the only really big number (10β-β1ββ₯β9). | [
{
"input": "12 1",
"output": "3"
},
{
"input": "25 20",
"output": "0"
},
{
"input": "10 9",
"output": "1"
},
{
"input": "300 1000",
"output": "0"
},
{
"input": "500 1000",
"output": "0"
},
{
"input": "1000 2000",
"output": "0"
},
{
"input": "10000 1000",
"output": "8991"
},
{
"input": "1000000000000000000 1000000000000000000",
"output": "0"
},
{
"input": "1000000000000000000 100000000000000000",
"output": "899999999999999991"
},
{
"input": "1000000000000000000 10000000000000000",
"output": "989999999999999991"
},
{
"input": "1000000000000000000 1000000000000000",
"output": "998999999999999991"
},
{
"input": "1000000000000000000 100000000000000",
"output": "999899999999999991"
},
{
"input": "1000000000000000000 200000000000000000",
"output": "799999999999999991"
},
{
"input": "10 5",
"output": "1"
},
{
"input": "20 5",
"output": "11"
},
{
"input": "20 9",
"output": "11"
},
{
"input": "100 9",
"output": "91"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "130 118",
"output": "1"
},
{
"input": "190 181",
"output": "0"
},
{
"input": "1999 1971",
"output": "10"
},
{
"input": "100 99",
"output": "1"
},
{
"input": "6909094398 719694282",
"output": "6189400069"
},
{
"input": "260 258",
"output": "0"
},
{
"input": "35 19",
"output": "6"
},
{
"input": "100 87",
"output": "1"
},
{
"input": "91 89",
"output": "0"
},
{
"input": "109 89",
"output": "10"
},
{
"input": "109 91",
"output": "10"
},
{
"input": "20331 11580",
"output": "8732"
},
{
"input": "405487470 255750281",
"output": "149737161"
},
{
"input": "17382 12863",
"output": "4493"
},
{
"input": "19725 14457",
"output": "5246"
},
{
"input": "24848 15384",
"output": "9449"
},
{
"input": "25727 15982",
"output": "9728"
},
{
"input": "109 90",
"output": "10"
},
{
"input": "1000000000000000000 999999999999999999",
"output": "1"
},
{
"input": "1000000000000000000 999999999999999998",
"output": "1"
},
{
"input": "1009 980",
"output": "10"
},
{
"input": "999999999999999999 999999999999999838",
"output": "0"
},
{
"input": "1000000000000000000 99999999999999800",
"output": "900000000000000061"
},
{
"input": "8785369357 3377262261",
"output": "5408107058"
},
{
"input": "110 109",
"output": "0"
},
{
"input": "999 777",
"output": "200"
},
{
"input": "327170000015578 77230000029054",
"output": "249939999986479"
},
{
"input": "12515000022229 1791000022317",
"output": "10723999999880"
},
{
"input": "9999999999999 9999999999882",
"output": "10"
},
{
"input": "213 196",
"output": "14"
},
{
"input": "92 82",
"output": "0"
},
{
"input": "148 136",
"output": "0"
},
{
"input": "8 9",
"output": "0"
},
{
"input": "309 299",
"output": "0"
},
{
"input": "9999 9963",
"output": "10"
},
{
"input": "82 81",
"output": "0"
},
{
"input": "9999999 9999936",
"output": "10"
},
{
"input": "171 155",
"output": "2"
},
{
"input": "999 972",
"output": "10"
},
{
"input": "999999999999 999999999891",
"output": "10"
},
{
"input": "9 9",
"output": "0"
},
{
"input": "6900 6885",
"output": "1"
},
{
"input": "96 57",
"output": "27"
},
{
"input": "5 4",
"output": "0"
},
{
"input": "17386 5814",
"output": "11557"
},
{
"input": "493679757404593 316259583979965",
"output": "177420173424564"
},
{
"input": "18474 9478",
"output": "8975"
},
{
"input": "270091571496186 250931112649966",
"output": "19160458846177"
},
{
"input": "565751690089037 381448507916936",
"output": "184303182172038"
},
{
"input": "19 10",
"output": "0"
}
] | 108 | 21,708,800 | 0 | 1,903 |
|
479 | Expression | [
"brute force",
"math"
] | null | null | Petya studies in a school and he adores Maths. His class has been studying arithmetic expressions. On the last class the teacher wrote three positive integers *a*, *b*, *c* on the blackboard. The task was to insert signs of operations '+' and '*', and probably brackets between the numbers so that the value of the resulting expression is as large as possible. Let's consider an example: assume that the teacher wrote numbers 1, 2 and 3 on the blackboard. Here are some ways of placing signs and brackets:
- 1+2*3=7 - 1*(2+3)=5 - 1*2*3=6 - (1+2)*3=9
Note that you can insert operation signs only between *a* and *b*, and between *b* and *c*, that is, you cannot swap integers. For instance, in the given sample you cannot get expression (1+3)*2.
It's easy to see that the maximum value that you can obtain is 9.
Your task is: given *a*, *b* and *c* print the maximum value that you can get. | The input contains three integers *a*, *b* and *c*, each on a single line (1<=β€<=*a*,<=*b*,<=*c*<=β€<=10). | Print the maximum value of the expression that you can obtain. | [
"1\n2\n3\n",
"2\n10\n3\n"
] | [
"9\n",
"60\n"
] | none | [
{
"input": "1\n2\n3",
"output": "9"
},
{
"input": "2\n10\n3",
"output": "60"
},
{
"input": "1\n1\n1",
"output": "3"
},
{
"input": "1\n2\n1",
"output": "4"
},
{
"input": "10\n10\n10",
"output": "1000"
},
{
"input": "5\n1\n3",
"output": "20"
},
{
"input": "3\n1\n5",
"output": "20"
},
{
"input": "6\n7\n1",
"output": "48"
},
{
"input": "1\n8\n3",
"output": "27"
},
{
"input": "9\n7\n2",
"output": "126"
},
{
"input": "1\n1\n10",
"output": "20"
},
{
"input": "9\n1\n1",
"output": "18"
},
{
"input": "10\n5\n6",
"output": "300"
},
{
"input": "8\n9\n7",
"output": "504"
},
{
"input": "4\n2\n10",
"output": "80"
},
{
"input": "2\n8\n3",
"output": "48"
},
{
"input": "3\n5\n7",
"output": "105"
},
{
"input": "1\n10\n1",
"output": "12"
},
{
"input": "2\n2\n2",
"output": "8"
},
{
"input": "5\n6\n1",
"output": "35"
},
{
"input": "10\n1\n1",
"output": "20"
},
{
"input": "1\n6\n1",
"output": "8"
},
{
"input": "1\n9\n1",
"output": "11"
},
{
"input": "2\n1\n2",
"output": "6"
},
{
"input": "2\n6\n1",
"output": "14"
},
{
"input": "9\n2\n1",
"output": "27"
},
{
"input": "1\n9\n2",
"output": "20"
},
{
"input": "1\n3\n1",
"output": "5"
},
{
"input": "2\n1\n1",
"output": "4"
}
] | 31 | 0 | -1 | 1,905 |
|
633 | Fibonacci-ish | [
"brute force",
"dp",
"hashing",
"implementation",
"math"
] | null | null | Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if
1. the sequence consists of at least two elements 1. *f*0 and *f*1 are arbitrary 1. *f**n*<=+<=2<==<=*f**n*<=+<=1<=+<=*f**n* for all *n*<=β₯<=0.
You are given some sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Your task is rearrange elements of this sequence in such a way that its longest possible prefix is Fibonacci-ish sequence. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=1000)Β β the length of the sequence *a**i*.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=109). | Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. | [
"3\n1 2 -1\n",
"5\n28 35 7 14 21\n"
] | [
"3\n",
"4\n"
] | In the first sample, if we rearrange elements of the sequence as β-β1, 2, 1, the whole sequence *a*<sub class="lower-index">*i*</sub> would be Fibonacci-ish.
In the second sample, the optimal way to rearrange elements is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/16f1f7e35511b29cb1396890ca2fb7dfa4d428de.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4003973f16750522e492d7d79318d7e2f0ff99cd.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/87b18fd9524b11e12faf154302fb14c1b55556fb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b8950ea952476baea26e03281fa2f7640b6241ef.png" style="max-width: 100.0%;max-height: 100.0%;"/>, 28. | [
{
"input": "3\n1 2 -1",
"output": "3"
},
{
"input": "5\n28 35 7 14 21",
"output": "4"
},
{
"input": "11\n-9 -1 -10 9 7 -4 0 -8 -3 3 5",
"output": "5"
},
{
"input": "10\n-4 -8 -8 8 -9 0 -7 9 1 0",
"output": "4"
},
{
"input": "2\n2 2",
"output": "2"
},
{
"input": "4\n1 -1 0 -2",
"output": "4"
},
{
"input": "2\n1000000000 1000000000",
"output": "2"
},
{
"input": "3\n1 1 2",
"output": "3"
},
{
"input": "5\n0 0 0 0 0",
"output": "5"
},
{
"input": "6\n1 -1 0 -1 -1 -2",
"output": "6"
},
{
"input": "5\n-7 0 -7 -7 -14",
"output": "5"
},
{
"input": "3\n0 -44 -49",
"output": "2"
},
{
"input": "5\n-1 1 0 0 0",
"output": "3"
},
{
"input": "2\n0 0",
"output": "2"
},
{
"input": "3\n0 0 0",
"output": "3"
},
{
"input": "4\n0 0 0 0",
"output": "4"
},
{
"input": "5\n0 0 0 0 0",
"output": "5"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 0",
"output": "10"
}
] | 140 | 1,228,800 | 0 | 1,909 |
|
909 | Segments | [
"constructive algorithms",
"math"
] | null | null | You are given an integer *N*. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and *N*, inclusive; there will be of them.
You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis.
Find the minimal number of layers you have to use for the given *N*. | The only input line contains a single integer *N* (1<=β€<=*N*<=β€<=100). | Output a single integer - the minimal number of layers required to draw the segments for the given *N*. | [
"2\n",
"3\n",
"4\n"
] | [
"2\n",
"4\n",
"6\n"
] | As an example, here are the segments and their optimal arrangement into layers for *N*β=β4. | [
{
"input": "2",
"output": "2"
},
{
"input": "3",
"output": "4"
},
{
"input": "4",
"output": "6"
},
{
"input": "21",
"output": "121"
},
{
"input": "100",
"output": "2550"
},
{
"input": "1",
"output": "1"
},
{
"input": "5",
"output": "9"
},
{
"input": "6",
"output": "12"
},
{
"input": "7",
"output": "16"
},
{
"input": "8",
"output": "20"
},
{
"input": "9",
"output": "25"
},
{
"input": "10",
"output": "30"
},
{
"input": "11",
"output": "36"
},
{
"input": "12",
"output": "42"
},
{
"input": "13",
"output": "49"
},
{
"input": "14",
"output": "56"
},
{
"input": "15",
"output": "64"
},
{
"input": "16",
"output": "72"
},
{
"input": "17",
"output": "81"
},
{
"input": "18",
"output": "90"
},
{
"input": "19",
"output": "100"
},
{
"input": "20",
"output": "110"
},
{
"input": "22",
"output": "132"
},
{
"input": "23",
"output": "144"
},
{
"input": "24",
"output": "156"
},
{
"input": "25",
"output": "169"
},
{
"input": "26",
"output": "182"
},
{
"input": "27",
"output": "196"
},
{
"input": "28",
"output": "210"
},
{
"input": "29",
"output": "225"
},
{
"input": "30",
"output": "240"
},
{
"input": "31",
"output": "256"
},
{
"input": "32",
"output": "272"
},
{
"input": "33",
"output": "289"
},
{
"input": "34",
"output": "306"
},
{
"input": "35",
"output": "324"
},
{
"input": "36",
"output": "342"
},
{
"input": "37",
"output": "361"
},
{
"input": "38",
"output": "380"
},
{
"input": "39",
"output": "400"
},
{
"input": "40",
"output": "420"
},
{
"input": "41",
"output": "441"
},
{
"input": "42",
"output": "462"
},
{
"input": "43",
"output": "484"
},
{
"input": "44",
"output": "506"
},
{
"input": "45",
"output": "529"
},
{
"input": "46",
"output": "552"
},
{
"input": "47",
"output": "576"
},
{
"input": "48",
"output": "600"
},
{
"input": "49",
"output": "625"
},
{
"input": "50",
"output": "650"
},
{
"input": "51",
"output": "676"
},
{
"input": "52",
"output": "702"
},
{
"input": "53",
"output": "729"
},
{
"input": "54",
"output": "756"
},
{
"input": "55",
"output": "784"
},
{
"input": "56",
"output": "812"
},
{
"input": "57",
"output": "841"
},
{
"input": "58",
"output": "870"
},
{
"input": "59",
"output": "900"
},
{
"input": "60",
"output": "930"
},
{
"input": "61",
"output": "961"
},
{
"input": "62",
"output": "992"
},
{
"input": "63",
"output": "1024"
},
{
"input": "64",
"output": "1056"
},
{
"input": "65",
"output": "1089"
},
{
"input": "66",
"output": "1122"
},
{
"input": "67",
"output": "1156"
},
{
"input": "68",
"output": "1190"
},
{
"input": "69",
"output": "1225"
},
{
"input": "70",
"output": "1260"
},
{
"input": "71",
"output": "1296"
},
{
"input": "72",
"output": "1332"
},
{
"input": "73",
"output": "1369"
},
{
"input": "74",
"output": "1406"
},
{
"input": "75",
"output": "1444"
},
{
"input": "76",
"output": "1482"
},
{
"input": "77",
"output": "1521"
},
{
"input": "78",
"output": "1560"
},
{
"input": "79",
"output": "1600"
},
{
"input": "80",
"output": "1640"
},
{
"input": "81",
"output": "1681"
},
{
"input": "82",
"output": "1722"
},
{
"input": "83",
"output": "1764"
},
{
"input": "84",
"output": "1806"
},
{
"input": "85",
"output": "1849"
},
{
"input": "86",
"output": "1892"
},
{
"input": "87",
"output": "1936"
},
{
"input": "88",
"output": "1980"
},
{
"input": "89",
"output": "2025"
},
{
"input": "90",
"output": "2070"
},
{
"input": "91",
"output": "2116"
},
{
"input": "92",
"output": "2162"
},
{
"input": "93",
"output": "2209"
},
{
"input": "94",
"output": "2256"
},
{
"input": "95",
"output": "2304"
},
{
"input": "96",
"output": "2352"
},
{
"input": "97",
"output": "2401"
},
{
"input": "98",
"output": "2450"
},
{
"input": "99",
"output": "2500"
},
{
"input": "1",
"output": "1"
},
{
"input": "5",
"output": "9"
}
] | 109 | 0 | 3 | 1,914 |
|
400 | Inna and Choose Options | [
"implementation"
] | null | null | There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a very unusual upgrade of this game. The rules of the game are given below:
There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: "X" or "O". Then the player chooses two positive integers *a* and *b* (*a*Β·*b*<==<=12), after that he makes a table of size *a*<=Γ<=*b* from the cards he put on the table as follows: the first *b* cards form the first row of the table, the second *b* cards form the second row of the table and so on, the last *b* cards form the last (number *a*) row of the table. The player wins if some column of the table contain characters "X" on all cards. Otherwise, the player loses.
Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers *a* and *b* to choose. Help her win the game: print to her all the possible ways of numbers *a*,<=*b* that she can choose and win. | The first line of the input contains integer *t* (1<=β€<=*t*<=β€<=100). This value shows the number of sets of test data in the input. Next follows the description of each of the *t* tests on a separate line.
The description of each test is a string consisting of 12 characters, each character is either "X", or "O". The *i*-th character of the string shows the character that is written on the *i*-th card from the start. | For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair *a*,<=*b*. Next, print on this line the pairs in the format *a*x*b*. Print the pairs in the order of increasing first parameter (*a*). Separate the pairs in the line by whitespaces. | [
"4\nOXXXOXOOXOOX\nOXOXOXOXOXOX\nXXXXXXXXXXXX\nOOOOOOOOOOOO\n"
] | [
"3 1x12 2x6 4x3\n4 1x12 2x6 3x4 6x2\n6 1x12 2x6 3x4 4x3 6x2 12x1\n0\n"
] | none | [
{
"input": "4\nOXXXOXOOXOOX\nOXOXOXOXOXOX\nXXXXXXXXXXXX\nOOOOOOOOOOOO",
"output": "3 1x12 2x6 4x3\n4 1x12 2x6 3x4 6x2\n6 1x12 2x6 3x4 4x3 6x2 12x1\n0"
},
{
"input": "2\nOOOOOOOOOOOO\nXXXXXXXXXXXX",
"output": "0\n6 1x12 2x6 3x4 4x3 6x2 12x1"
},
{
"input": "13\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXX",
"output": "6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1\n6 1x12 2x6 3x4 4x3 6x2 12x1"
}
] | 77 | 5,632,000 | 3 | 1,915 |
|
319 | Psychos in a Line | [
"data structures",
"implementation"
] | null | null | There are *n* psychos standing in a line. Each psycho is assigned a unique integer from 1 to *n*. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step.
You're given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise. | The first line of input contains integer *n* denoting the number of psychos, (1<=β€<=*n*<=β€<=105). In the second line there will be a list of *n* space separated distinct integers each in range 1 to *n*, inclusive β ids of the psychos in the line from left to right. | Print the number of steps, so that the line remains the same afterward. | [
"10\n10 9 7 8 6 5 3 4 2 1\n",
"6\n1 2 3 4 5 6\n"
] | [
"2\n",
"0\n"
] | In the first sample line of the psychos transforms as follows: [10 9 7 8 6 5 3 4 2 1] βββ [10 8 4] βββ [10]. So, there are two steps. | [
{
"input": "10\n10 9 7 8 6 5 3 4 2 1",
"output": "2"
},
{
"input": "6\n1 2 3 4 5 6",
"output": "0"
},
{
"input": "6\n6 5 4 3 2 1",
"output": "1"
},
{
"input": "10\n10 7 4 2 5 8 9 6 3 1",
"output": "4"
},
{
"input": "15\n15 9 5 10 7 11 14 6 2 3 12 1 8 13 4",
"output": "4"
},
{
"input": "100\n61 96 25 10 50 71 38 77 76 75 59 100 89 66 6 99 2 13 3 23 91 93 22 92 4 86 90 44 39 31 9 47 28 95 18 54 1 73 94 78 60 20 42 84 97 83 16 81 67 64 74 46 82 5 88 80 14 48 53 79 30 11 62 21 41 70 63 58 51 56 57 17 87 72 27 85 68 49 52 8 12 98 43 37 35 69 55 32 26 40 29 65 19 24 34 33 15 45 36 7",
"output": "8"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n1 2",
"output": "0"
},
{
"input": "2\n2 1",
"output": "1"
}
] | 62 | 0 | 0 | 1,916 |
|
978 | Bus Video System | [
"combinatorics",
"math"
] | null | null | The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.
If $x$ is the number of passengers in a bus just before the current bus stop and $y$ is the number of passengers in the bus just after current bus stop, the system records the number $y-x$. So the system records show how number of passengers changed.
The test run was made for single bus and $n$ bus stops. Thus, the system recorded the sequence of integers $a_1, a_2, \dots, a_n$ (exactly one number for each bus stop), where $a_i$ is the record for the bus stop $i$. The bus stops are numbered from $1$ to $n$ in chronological order.
Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$ (that is, at any time in the bus there should be from $0$ to $w$ passengers inclusive). | The first line contains two integers $n$ and $w$ $(1 \le n \le 1\,000, 1 \le w \le 10^{9})$ β the number of bus stops and the capacity of the bus.
The second line contains a sequence $a_1, a_2, \dots, a_n$ $(-10^{6} \le a_i \le 10^{6})$, where $a_i$ equals to the number, which has been recorded by the video system after the $i$-th bus stop. | Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to $w$. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0. | [
"3 5\n2 1 -3\n",
"2 4\n-1 1\n",
"4 10\n2 4 1 2\n"
] | [
"3\n",
"4\n",
"2\n"
] | In the first example initially in the bus could be $0$, $1$ or $2$ passengers.
In the second example initially in the bus could be $1$, $2$, $3$ or $4$ passengers.
In the third example initially in the bus could be $0$ or $1$ passenger. | [
{
"input": "3 5\n2 1 -3",
"output": "3"
},
{
"input": "2 4\n-1 1",
"output": "4"
},
{
"input": "4 10\n2 4 1 2",
"output": "2"
},
{
"input": "2 10\n-1 2",
"output": "9"
},
{
"input": "3 4\n-3 -4 4",
"output": "0"
},
{
"input": "10 1\n-1 -1 3 -4 2 3 0 -3 3 2",
"output": "0"
},
{
"input": "5 21\n-3 2 -4 -1 -5",
"output": "11"
},
{
"input": "5 9\n-2 -1 2 -1 -2",
"output": "6"
},
{
"input": "8 7\n-5 0 -3 1 -1 5 0 -6",
"output": "0"
},
{
"input": "3 4\n-2 -1 0",
"output": "2"
},
{
"input": "1 1000000000\n0",
"output": "1000000001"
},
{
"input": "2 1000000000\n-1000000 -1000000",
"output": "998000001"
},
{
"input": "2 1000000000\n1000000 -1000000",
"output": "999000001"
},
{
"input": "2 1000000000\n-1000000 1000000",
"output": "999000001"
},
{
"input": "2 1000000000\n1000000 1000000",
"output": "998000001"
},
{
"input": "102 1\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "0"
},
{
"input": "1 1234564\n1",
"output": "1234564"
},
{
"input": "3 4\n1 2 3",
"output": "0"
}
] | 46 | 0 | 0 | 1,924 |
|
15 | Laser | [
"math"
] | B. Laser | 1 | 64 | Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of *n*<=Γ<=*m* cells and a robotic arm. Each cell of the field is a 1<=Γ<=1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized β if you move one of the lasers by a vector, another one moves by the same vector.
The following facts about the experiment are known:
- initially the whole field is covered with a chocolate bar of the size *n*<=Γ<=*m*, both lasers are located above the field and are active; - the chocolate melts within one cell of the field at which the laser is pointed; - all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; - at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman.
You are given *n*, *m* and the cells (*x*1,<=*y*1) and (*x*2,<=*y*2), where the lasers are initially pointed at (*x**i* is a column number, *y**i* is a row number). Rows are numbered from 1 to *m* from top to bottom and columns are numbered from 1 to *n* from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions. | The first line contains one integer number *t* (1<=β€<=*t*<=β€<=10000) β the number of test sets. Each of the following *t* lines describes one test set. Each line contains integer numbers *n*, *m*, *x*1, *y*1, *x*2, *y*2, separated by a space (2<=β€<=*n*,<=*m*<=β€<=109, 1<=β€<=*x*1,<=*x*2<=β€<=*n*, 1<=β€<=*y*1,<=*y*2<=β€<=*m*). Cells (*x*1,<=*y*1) and (*x*2,<=*y*2) are distinct. | Each of the *t* lines of the output should contain the answer to the corresponding input test set. | [
"2\n4 4 1 1 3 3\n4 3 1 1 2 2\n"
] | [
"8\n2\n"
] | none | [
{
"input": "2\n4 4 1 1 3 3\n4 3 1 1 2 2",
"output": "8\n2"
},
{
"input": "1\n2 2 1 2 2 1",
"output": "2"
},
{
"input": "1\n2 2 1 2 2 1",
"output": "2"
},
{
"input": "1\n3 3 3 2 1 1",
"output": "5"
},
{
"input": "1\n3 4 1 1 1 2",
"output": "0"
},
{
"input": "1\n4 3 3 1 4 1",
"output": "0"
},
{
"input": "1\n3 5 2 4 3 5",
"output": "2"
},
{
"input": "1\n4 5 2 2 4 2",
"output": "0"
},
{
"input": "1\n2 5 1 5 2 2",
"output": "6"
},
{
"input": "1\n2 6 2 6 2 3",
"output": "0"
},
{
"input": "1\n3 6 3 5 2 4",
"output": "2"
},
{
"input": "1\n4 6 2 1 2 3",
"output": "0"
},
{
"input": "1\n5 6 3 4 4 2",
"output": "4"
},
{
"input": "1\n7 3 6 2 5 2",
"output": "0"
},
{
"input": "1\n8 2 6 1 7 2",
"output": "2"
},
{
"input": "1\n9 6 6 5 3 1",
"output": "30"
},
{
"input": "20\n100 200 100 1 100 100\n100 200 1 100 100 100\n2 2 1 1 2 2\n100 100 50 50 1 1\n10 10 5 5 1 1\n100 100 99 1 1 99\n100 100 1 99 99 1\n100 100 1 10 10 1\n100 100 1 1 10 10\n9 6 1 3 3 1\n1000000000 1000000000 1 1 1000000000 1000000000\n9 4 1 4 4 1\n6 4 1 1 5 4\n6 2 1 1 5 2\n8 2 1 1 5 2\n10 2 1 1 5 2\n10 2 1 1 3 2\n4 3 1 1 2 2\n3 3 1 1 2 2\n3 3 1 1 2 1",
"output": "0\n19600\n2\n4802\n32\n9992\n9992\n162\n162\n8\n999999999999999998\n24\n20\n8\n8\n8\n4\n2\n2\n0"
}
] | 310 | 0 | 3.845 | 1,933 |
394 | Counting Sticks | [
"brute force",
"implementation"
] | null | null | When new students come to the Specialized Educational and Scientific Centre (SESC) they need to start many things from the beginning. Sometimes the teachers say (not always unfairly) that we cannot even count. So our teachers decided to teach us arithmetics from the start. And what is the best way to teach students add and subtract? β That's right, using counting sticks! An here's our new task:
An expression of counting sticks is an expression of type:
Sign + consists of two crossed sticks: one vertical and one horizontal. Sign = consists of two horizontal sticks. The expression is arithmetically correct if *A*<=+<=*B*<==<=*C*.
We've got an expression that looks like *A*<=+<=*B*<==<=*C* given by counting sticks. Our task is to shift at most one stick (or we can shift nothing) so that the expression became arithmetically correct. Note that we cannot remove the sticks from the expression, also we cannot shift the sticks from the signs + and =.
We really aren't fabulous at arithmetics. Can you help us? | The single line contains the initial expression. It is guaranteed that the expression looks like *A*<=+<=*B*<==<=*C*, where 1<=β€<=*A*,<=*B*,<=*C*<=β€<=100. | If there isn't a way to shift the stick so the expression becomes correct, print on a single line "Impossible" (without the quotes). If there is a way, print the resulting expression. Follow the format of the output from the test samples. Don't print extra space characters.
If there are multiple correct answers, print any of them. For clarifications, you are recommended to see the test samples. | [
"||+|=|||||\n",
"|||||+||=||\n",
"|+|=||||||\n",
"||||+||=||||||\n"
] | [
"|||+|=||||\n",
"Impossible\n",
"Impossible\n",
"||||+||=||||||\n"
] | In the first sample we can shift stick from the third group of sticks to the first one.
In the second sample we cannot shift vertical stick from + sign to the second group of sticks. So we cannot make a - sign.
There is no answer in the third sample because we cannot remove sticks from the expression.
In the forth sample the initial expression is already arithmetically correct and that is why we don't have to shift sticks. | [
{
"input": "||+|=|||||",
"output": "|||+|=||||"
},
{
"input": "|||||+||=||",
"output": "Impossible"
},
{
"input": "|+|=||||||",
"output": "Impossible"
},
{
"input": "||||+||=||||||",
"output": "||||+||=||||||"
},
{
"input": "||||||||||||+|||||||||||=||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "||||||||||||||||||+||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "|||||||||||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||||||=|||||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+|=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+|=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "|+|=|",
"output": "Impossible"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "|||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "|||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "|||||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||||||||||||||||=|",
"output": "Impossible"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=|",
"output": "Impossible"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|||||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||||=|",
"output": "Impossible"
},
{
"input": "|||||||||||||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||||||||||||||||||||||=|",
"output": "Impossible"
},
{
"input": "||+||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|+||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||+|=|",
"output": "|+|=||"
},
{
"input": "|+||=|",
"output": "|+|=||"
},
{
"input": "|+|=||",
"output": "|+|=||"
},
{
"input": "|||+|=|",
"output": "Impossible"
},
{
"input": "|||+|=|",
"output": "Impossible"
},
{
"input": "|||||||||||||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "||||||||||||||||||||||||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||+||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|+||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "Impossible"
},
{
"input": "|+|=|||",
"output": "Impossible"
},
{
"input": "|+|=||||",
"output": "||+|=|||"
},
{
"input": "|+||=|",
"output": "|+|=||"
},
{
"input": "|+||||||=|||||",
"output": "|+|||||=||||||"
},
{
"input": "|+|||=||",
"output": "|+||=|||"
},
{
"input": "|+||||=|||",
"output": "|+|||=||||"
},
{
"input": "|+|||||=||||",
"output": "|+||||=|||||"
},
{
"input": "||+||=||",
"output": "|+||=|||"
},
{
"input": "||+|||=|||",
"output": "|+|||=||||"
},
{
"input": "|||||+||=||||||||||",
"output": "Impossible"
},
{
"input": "|||+|||=||||",
"output": "||+|||=|||||"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "|||||+|||||=||||||||||||",
"output": "||||||+|||||=|||||||||||"
},
{
"input": "||+|||||||=|||||||",
"output": "|+|||||||=||||||||"
},
{
"input": "||||+||||=||||||",
"output": "|||+||||=|||||||"
},
{
"input": "||||+|||=|||||",
"output": "|||+|||=||||||"
},
{
"input": "||+|||=|||||||",
"output": "|||+|||=||||||"
},
{
"input": "||+|=|",
"output": "|+|=||"
},
{
"input": "|||+|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "||+|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
},
{
"input": "||+|=||||||",
"output": "Impossible"
},
{
"input": "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||+||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||",
"output": "Impossible"
}
] | 108 | 0 | 0 | 1,936 |
|
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"
}
] | 61 | 0 | -1 | 1,937 |
|
519 | A and B and Team Training | [
"greedy",
"implementation",
"math",
"number theory"
] | null | null | A and B are preparing themselves for programming contests.
An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.
A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.
However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.
As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.
There are *n* experienced members and *m* newbies on the training session. Can you calculate what maximum number of teams can be formed? | The first line contains two integers *n* and *m* (0<=β€<=*n*,<=*m*<=β€<=5Β·105) β the number of experienced participants and newbies that are present at the training session. | Print the maximum number of teams that can be formed. | [
"2 6\n",
"4 5\n"
] | [
"2\n",
"3\n"
] | Let's represent the experienced players as XP and newbies as NB.
In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).
In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB). | [
{
"input": "2 6",
"output": "2"
},
{
"input": "4 5",
"output": "3"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "3 3",
"output": "2"
},
{
"input": "500000 500000",
"output": "333333"
},
{
"input": "70 100",
"output": "56"
},
{
"input": "5 12525",
"output": "5"
},
{
"input": "10 5",
"output": "5"
},
{
"input": "5 10",
"output": "5"
},
{
"input": "0 0",
"output": "0"
},
{
"input": "0 1",
"output": "0"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "0 21233",
"output": "0"
},
{
"input": "12523 0",
"output": "0"
},
{
"input": "1231 1253",
"output": "828"
},
{
"input": "500000 0",
"output": "0"
},
{
"input": "1 500000",
"output": "1"
},
{
"input": "250000 500000",
"output": "250000"
},
{
"input": "500000 250000",
"output": "250000"
},
{
"input": "33333 77777",
"output": "33333"
},
{
"input": "30900 174529",
"output": "30900"
},
{
"input": "89979 57154",
"output": "49044"
},
{
"input": "231646 398487",
"output": "210044"
},
{
"input": "332019 281112",
"output": "204377"
},
{
"input": "473686 122443",
"output": "122443"
},
{
"input": "481245 86879",
"output": "86879"
},
{
"input": "39935 123534",
"output": "39935"
},
{
"input": "10000 20000",
"output": "10000"
},
{
"input": "10000 20001",
"output": "10000"
},
{
"input": "10000 20002",
"output": "10000"
},
{
"input": "10000 20003",
"output": "10000"
},
{
"input": "10000 20004",
"output": "10000"
},
{
"input": "10001 20000",
"output": "10000"
},
{
"input": "10001 20001",
"output": "10000"
},
{
"input": "10001 20002",
"output": "10001"
},
{
"input": "10001 20003",
"output": "10001"
},
{
"input": "10001 20004",
"output": "10001"
},
{
"input": "20000 10000",
"output": "10000"
},
{
"input": "20001 10000",
"output": "10000"
},
{
"input": "20002 10000",
"output": "10000"
},
{
"input": "20003 10000",
"output": "10000"
},
{
"input": "20004 10000",
"output": "10000"
},
{
"input": "20000 10001",
"output": "10000"
},
{
"input": "20001 10001",
"output": "10000"
},
{
"input": "20002 10001",
"output": "10001"
},
{
"input": "20003 10001",
"output": "10001"
},
{
"input": "20004 10001",
"output": "10001"
},
{
"input": "10 0",
"output": "0"
},
{
"input": "0 6",
"output": "0"
},
{
"input": "2 3",
"output": "1"
},
{
"input": "1 2",
"output": "1"
},
{
"input": "0 0",
"output": "0"
}
] | 109 | 0 | 3 | 1,939 |
|
567 | President and Roads | [
"dfs and similar",
"graphs",
"hashing",
"shortest paths"
] | null | null | Berland has *n* cities, the capital is located in city *s*, and the historic home town of the President is in city *t* (*s*<=β <=*t*). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.
Once a year the President visited his historic home town *t*, for which his motorcade passes along some path from *s* to *t* (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from *s* to *t*, along which he will travel the fastest.
The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer. | The first lines contain four integers *n*, *m*, *s* and *t* (2<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=105;Β 1<=β€<=*s*,<=*t*<=β€<=*n*) β the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (*s*<=β <=*t*).
Next *m* lines contain the roads. Each road is given as a group of three integers *a**i*,<=*b**i*,<=*l**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*;Β *a**i*<=β <=*b**i*;Β 1<=β€<=*l**i*<=β€<=106) β the cities that are connected by the *i*-th road and the time needed to ride along it. The road is directed from city *a**i* to city *b**i*.
The cities are numbered from 1 to *n*. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from *s* to *t* along the roads. | Print *m* lines. The *i*-th line should contain information about the *i*-th road (the roads are numbered in the order of appearance in the input).
If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes).
Otherwise, if the *i*-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing.
If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes). | [
"6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1\n",
"3 3 1 3\n1 2 10\n2 3 10\n1 3 100\n",
"2 2 1 2\n1 2 1\n1 2 2\n"
] | [
"YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES\n",
"YES\nYES\nCAN 81\n",
"YES\nNO\n"
] | The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing.
In the first sample president initially may choose one of the two following ways for a ride: 1βββ2βββ4βββ5βββ6 or 1βββ2βββ3βββ5βββ6. | [
{
"input": "6 7 1 6\n1 2 2\n1 3 10\n2 3 7\n2 4 8\n3 5 3\n4 5 2\n5 6 1",
"output": "YES\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES"
},
{
"input": "3 3 1 3\n1 2 10\n2 3 10\n1 3 100",
"output": "YES\nYES\nCAN 81"
},
{
"input": "2 2 1 2\n1 2 1\n1 2 2",
"output": "YES\nNO"
},
{
"input": "2 1 1 2\n1 2 1",
"output": "YES"
},
{
"input": "3 3 1 3\n1 2 10\n2 3 10\n1 3 19",
"output": "CAN 2\nCAN 2\nYES"
},
{
"input": "4 3 1 4\n1 2 1\n2 3 1\n3 4 1",
"output": "YES\nYES\nYES"
},
{
"input": "4 4 1 4\n1 2 1\n2 3 1\n3 4 1\n1 3 2",
"output": "NO\nNO\nYES\nCAN 1"
},
{
"input": "4 4 1 4\n1 2 1\n2 3 1\n3 4 1\n1 3 1",
"output": "NO\nNO\nYES\nYES"
},
{
"input": "6 6 1 6\n1 2 2\n2 3 4\n2 4 3\n3 5 2\n4 5 3\n5 6 10",
"output": "YES\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nYES"
},
{
"input": "6 6 1 6\n1 2 2\n2 3 3\n2 4 3\n3 5 2\n4 5 3\n5 6 10",
"output": "YES\nYES\nCAN 2\nYES\nCAN 2\nYES"
},
{
"input": "2 1 1 2\n1 2 1",
"output": "YES"
},
{
"input": "2 2 1 2\n1 2 6\n1 2 6",
"output": "CAN 1\nCAN 1"
},
{
"input": "2 3 1 2\n1 2 7\n1 2 7\n1 2 7",
"output": "CAN 1\nCAN 1\nCAN 1"
},
{
"input": "2 10 1 2\n1 2 5\n1 2 5\n1 2 7\n1 2 5\n1 2 6\n1 2 5\n1 2 5\n1 2 6\n1 2 5\n1 2 6",
"output": "CAN 1\nCAN 1\nCAN 3\nCAN 1\nCAN 2\nCAN 1\nCAN 1\nCAN 2\nCAN 1\nCAN 2"
},
{
"input": "3 2 1 2\n3 2 3\n1 3 6",
"output": "YES\nYES"
},
{
"input": "3 3 1 3\n2 3 7\n2 3 7\n1 2 6",
"output": "CAN 1\nCAN 1\nYES"
},
{
"input": "3 4 3 1\n2 1 4\n2 1 2\n3 2 1\n2 1 2",
"output": "CAN 3\nCAN 1\nYES\nCAN 1"
},
{
"input": "3 5 1 2\n1 3 3\n1 2 9\n3 2 6\n1 2 10\n1 3 3",
"output": "CAN 1\nCAN 1\nCAN 1\nCAN 2\nCAN 1"
},
{
"input": "3 7 1 3\n1 3 11\n1 3 12\n1 2 2\n1 3 11\n1 2 2\n2 3 9\n2 3 9",
"output": "CAN 1\nCAN 2\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nCAN 1"
},
{
"input": "5 7 3 2\n5 4 8\n3 1 2\n1 2 20\n1 5 8\n4 2 4\n1 5 8\n5 4 9",
"output": "CAN 1\nYES\nCAN 1\nCAN 1\nCAN 1\nCAN 1\nCAN 2"
},
{
"input": "7 8 5 3\n4 3 5\n7 1 8\n2 1 16\n2 7 7\n2 6 21\n5 2 10\n6 4 4\n1 6 5",
"output": "YES\nYES\nCAN 2\nYES\nCAN 2\nYES\nYES\nYES"
},
{
"input": "6 8 1 6\n1 2 13\n3 2 3\n4 5 6\n1 6 28\n1 3 10\n1 4 18\n2 4 4\n5 6 4",
"output": "CAN 1\nCAN 1\nYES\nCAN 2\nCAN 1\nCAN 2\nYES\nYES"
},
{
"input": "7 10 4 7\n6 3 9\n2 1 4\n3 7 3\n5 2 6\n1 3 12\n5 2 6\n4 5 4\n4 5 3\n1 6 3\n4 6 16",
"output": "CAN 1\nCAN 1\nYES\nCAN 1\nCAN 1\nCAN 1\nCAN 2\nCAN 1\nCAN 1\nCAN 1"
},
{
"input": "10 13 2 10\n7 3 5\n6 1 10\n9 6 4\n4 10 48\n9 5 2\n1 10 3\n5 6 2\n7 6 19\n4 8 8\n2 4 8\n8 7 7\n7 6 20\n3 9 10",
"output": "CAN 1\nYES\nCAN 1\nCAN 2\nCAN 1\nYES\nCAN 1\nCAN 1\nYES\nYES\nYES\nCAN 2\nCAN 1"
},
{
"input": "4 4 1 4\n1 2 1\n2 3 1\n3 4 1\n1 4 3",
"output": "NO\nNO\nNO\nCAN 1"
},
{
"input": "5 6 1 5\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1",
"output": "NO\nCAN 3\nCAN 3\nYES\nYES\nYES"
},
{
"input": "5 6 1 5\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1",
"output": "NO\nCAN 3\nCAN 3\nYES\nYES\nYES"
},
{
"input": "2 1 1 2\n1 2 1",
"output": "YES"
},
{
"input": "3 3 1 3\n1 2 1\n1 3 2\n2 3 1",
"output": "NO\nCAN 1\nNO"
},
{
"input": "10 10 1 10\n1 5 178\n1 8 221\n2 7 92\n2 8 159\n3 5 55\n3 6 179\n3 10 237\n4 8 205\n5 6 191\n8 10 157",
"output": "NO\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nYES"
},
{
"input": "10 10 1 10\n1 4 201\n2 3 238\n3 4 40\n3 6 231\n3 8 45\n4 5 227\n4 6 58\n4 9 55\n5 7 14\n6 10 242",
"output": "YES\nNO\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nYES"
},
{
"input": "3 3 1 3\n1 2 1\n2 3 1\n1 3 2",
"output": "NO\nNO\nCAN 1"
},
{
"input": "6 7 1 6\n1 2 1000000\n2 3 1000000\n2 5 1000000\n1 3 1000000\n3 5 1000000\n2 4 1000000\n5 6 1000000",
"output": "CAN 1\nNO\nCAN 1\nCAN 1\nCAN 1\nNO\nYES"
},
{
"input": "2 1 1 2\n1 2 1000000",
"output": "YES"
},
{
"input": "2 2 1 2\n1 2 1000000\n1 2 1000000",
"output": "CAN 1\nCAN 1"
},
{
"input": "2 2 1 2\n1 2 1000000\n1 2 1000000",
"output": "CAN 1\nCAN 1"
},
{
"input": "2 9 1 2\n1 2 1000000\n1 2 1000000\n1 2 1000000\n1 2 1000000\n1 2 1\n1 2 1000000\n1 2 1000000\n1 2 1000000\n1 2 1000000",
"output": "NO\nNO\nNO\nNO\nYES\nNO\nNO\nNO\nNO"
},
{
"input": "2 9 1 2\n1 2 1000000\n1 2 1000000\n1 2 1000000\n1 2 1000000\n1 2 2\n1 2 1000000\n1 2 1000000\n1 2 1000000\n1 2 1000000",
"output": "CAN 999999\nCAN 999999\nCAN 999999\nCAN 999999\nYES\nCAN 999999\nCAN 999999\nCAN 999999\nCAN 999999"
},
{
"input": "3 2 1 3\n1 3 1\n1 2 1",
"output": "YES\nNO"
},
{
"input": "4 5 1 4\n1 2 1\n1 2 1\n2 3 1\n3 4 1\n3 4 1",
"output": "NO\nNO\nYES\nNO\nNO"
},
{
"input": "3 3 1 3\n1 2 666\n2 3 555\n3 1 1",
"output": "YES\nYES\nNO"
}
] | 46 | 0 | 0 | 1,950 |
|
260 | Balls and Boxes | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Little Vasya had *n* boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to *n* from left to right.
Once Vasya chose one of the boxes, let's assume that its number is *i*, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers *i*<=+<=1, *i*<=+<=2, *i*<=+<=3 and so on. If Vasya puts a ball into the box number *n*, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number *i*. If *i*<==<=*n*, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if *i*<==<=3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4,<=1,<=2,<=3,<=4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number *x* β the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes. | The first line of the input contains two integers *n* and *x* (2<=β€<=*n*<=β€<=105, 1<=β€<=*x*<=β€<=*n*), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*, where integer *a**i* (0<=β€<=*a**i*<=β€<=109, *a**x*<=β <=0) represents the number of balls in the box with index *i* after Vasya completes all the actions.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | Print *n* integers, where the *i*-th one represents the number of balls in the box number *i* before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them. | [
"4 4\n4 3 1 6\n",
"5 2\n3 2 0 2 7\n",
"3 3\n2 3 1\n"
] | [
"3 2 5 4 ",
"2 1 4 1 6 ",
"1 2 3 "
] | none | [
{
"input": "4 4\n4 3 1 6",
"output": "3 2 5 4 "
},
{
"input": "5 2\n3 2 0 2 7",
"output": "2 1 4 1 6 "
},
{
"input": "3 3\n2 3 1",
"output": "1 2 3 "
},
{
"input": "10 3\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000",
"output": "0 0 10000000000 0 0 0 0 0 0 0 "
},
{
"input": "5 4\n0 554459682 978416312 784688178 954779973",
"output": "3 554459681 978416311 784688177 954779973 "
},
{
"input": "5 2\n1 554459683 978416312 784688178 954779974",
"output": "6 554459681 978416311 784688177 954779973 "
},
{
"input": "10 8\n994538714 617271264 168716105 915909382 338220996 533154890 507276501 323171960 121635370 33140162",
"output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401628 "
},
{
"input": "10 5\n994538715 617271265 168716106 915909383 338220997 533154890 507276501 323171960 121635371 33140163",
"output": "961398551 584131101 135575942 882769219 305080833 500014727 474136338 290031797 88495208 331401635 "
},
{
"input": "15 12\n256121252 531930087 157210108 921323934 786210452 0 962820592 824495629 642702951 556399489 660627699 454443499 406577817 234814732 387536495",
"output": "256121252 531930087 157210108 921323934 786210452 6 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "
},
{
"input": "15 8\n256121253 531930088 157210109 921323935 786210453 1 962820593 824495630 642702951 556399489 660627699 454443499 406577818 234814733 387536496",
"output": "256121252 531930087 157210108 921323934 786210452 17 962820591 824495628 642702950 556399488 660627698 454443498 406577817 234814732 387536495 "
},
{
"input": "48 34\n227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 0 730509325 68523190 726721460 85037180 620964625 219537305 396613042 39840356 91947418 566463810 791505982 87036026 446282153 912204581 895882687 284561729 35994526 423462628",
"output": "227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 4 730509324 68523189 726721459 85037179 620964625 219537305 396613042 39840356 91947418 566463810 791505982 87036026 446282153 912204581 895882687 284561729 35994526 423462628 "
},
{
"input": "48 19\n227460648 746912227 53993110 682685526 621533699 666833118 492590399 167395932 678377837 66509685 638633256 713194370 386921921 34175133 704550052 220688092 499436761 495071386 102952102 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 0 730509325 68523190 726721460 85037180 620964626 219537306 396613043 39840357 91947419 566463811 791505983 87036027 446282154 912204582 895882688 284561730 35994527 423462629",
"output": "227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 946682102 237312198 943872065 957150897 357568282 367006748 37 730509324 68523189 726721459 85037179 620964625 219537305 396613042 39840356 91947418 566463810 791505982 87036026 446282153 912204581 895882687 284561729 35994526 423462628 "
},
{
"input": "10 5\n3 3 3 3 4 3 3 3 3 3",
"output": "0 0 0 31 0 0 0 0 0 0 "
},
{
"input": "5 4\n3 1 3 1 3",
"output": "2 0 2 5 2 "
}
] | 62 | 2,867,200 | -1 | 1,955 |
|
983 | Finite or not? | [
"implementation",
"math"
] | null | null | You are given several queries. Each query consists of three integers $p$, $q$ and $b$. You need to answer whether the result of $p/q$ in notation with base $b$ is a finite fraction.
A fraction in notation with base $b$ is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point. | The first line contains a single integer $n$ ($1 \le n \le 10^5$)Β β the number of queries.
Next $n$ lines contain queries, one per line. Each line contains three integers $p$, $q$, and $b$ ($0 \le p \le 10^{18}$, $1 \le q \le 10^{18}$, $2 \le b \le 10^{18}$). All numbers are given in notation with base $10$. | For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise. | [
"2\n6 12 10\n4 3 10\n",
"4\n1 1 2\n9 36 2\n4 12 3\n3 5 4\n"
] | [
"Finite\nInfinite\n",
"Finite\nFinite\nFinite\nInfinite\n"
] | $\frac{6}{12} = \frac{1}{2} = 0,5_{10}$
$\frac{4}{3} = 1,(3)_{10}$
$\frac{9}{36} = \frac{1}{4} = 0,01_2$
$\frac{4}{12} = \frac{1}{3} = 0,1_3$ | [
{
"input": "2\n6 12 10\n4 3 10",
"output": "Finite\nInfinite"
},
{
"input": "4\n1 1 2\n9 36 2\n4 12 3\n3 5 4",
"output": "Finite\nFinite\nFinite\nInfinite"
},
{
"input": "10\n10 5 3\n1 7 10\n7 5 7\n4 4 9\n6 5 2\n6 7 5\n9 9 7\n7 5 5\n6 6 4\n10 8 2",
"output": "Finite\nInfinite\nInfinite\nFinite\nInfinite\nInfinite\nFinite\nFinite\nFinite\nFinite"
},
{
"input": "10\n1 3 10\n6 2 6\n2 3 9\n7 8 4\n5 6 10\n1 2 7\n0 3 6\n9 3 4\n4 4 9\n10 9 10",
"output": "Infinite\nFinite\nFinite\nFinite\nInfinite\nInfinite\nFinite\nFinite\nFinite\nInfinite"
},
{
"input": "10\n10 8 5\n0 6 9\n0 7 6\n5 7 3\n7 6 8\n0 4 8\n2 6 3\n10 2 9\n6 7 9\n9 1 4",
"output": "Infinite\nFinite\nFinite\nInfinite\nInfinite\nFinite\nFinite\nFinite\nInfinite\nFinite"
},
{
"input": "10\n5 8 2\n0 5 8\n5 9 7\n0 7 2\n6 7 2\n10 3 7\n8 1 10\n9 1 8\n0 7 10\n9 1 4",
"output": "Finite\nFinite\nInfinite\nFinite\nInfinite\nInfinite\nFinite\nFinite\nFinite\nFinite"
},
{
"input": "1\n1 864691128455135232 2",
"output": "Infinite"
},
{
"input": "11\n1 1000000000000000000 10000000\n2 999 9\n2 999 333111\n0 9 7\n17 128 2\n13 311992186885373952 18\n1971402979058461 750473176484995605 75\n14 19 23\n3 21914624432020321 23\n3 21914624432020321 46\n3 21914624432020321 47",
"output": "Finite\nInfinite\nFinite\nFinite\nFinite\nFinite\nFinite\nInfinite\nFinite\nFinite\nInfinite"
},
{
"input": "1\n1 100000000000000000 10000000000000000",
"output": "Finite"
},
{
"input": "1\n1 4294967297 4294967296",
"output": "Infinite"
},
{
"input": "1\n1 5244319080000 30030",
"output": "Finite"
}
] | 0 | 0 | -1 | 1,961 |
|
776 | A Serial Killer | [
"brute force",
"implementation",
"strings"
] | null | null | Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern. | First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer *n* (1<=β€<=*n*<=β€<=1000), the number of days.
Next *n* lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters. | Output *n*<=+<=1 lines, the *i*-th line should contain the two persons from which the killer selects for the *i*-th murder. The (*n*<=+<=1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order. | [
"ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler\n",
"icm codeforces\n1\ncodeforces technex\n"
] | [
"ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler\n",
"icm codeforces\nicm technex\n"
] | In first example, the killer starts with ross and rachel.
- After day 1, ross is killed and joey appears. - After day 2, rachel is killed and phoebe appears. - After day 3, phoebe is killed and monica appears. - After day 4, monica is killed and chandler appears. | [
{
"input": "ross rachel\n4\nross joey\nrachel phoebe\nphoebe monica\nmonica chandler",
"output": "ross rachel\njoey rachel\njoey phoebe\njoey monica\njoey chandler"
},
{
"input": "icm codeforces\n1\ncodeforces technex",
"output": "icm codeforces\nicm technex"
},
{
"input": "a b\n3\na c\nb d\nd e",
"output": "a b\nc b\nc d\nc e"
},
{
"input": "ze udggmyop\n4\nze szhrbmft\nudggmyop mjorab\nszhrbmft ojdtfnzxj\nojdtfnzxj yjlkg",
"output": "ze udggmyop\nszhrbmft udggmyop\nszhrbmft mjorab\nojdtfnzxj mjorab\nyjlkg mjorab"
},
{
"input": "q s\n10\nq b\nb j\ns g\nj f\nf m\ng c\nc a\nm d\nd z\nz o",
"output": "q s\nb s\nj s\nj g\nf g\nm g\nm c\nm a\nd a\nz a\no a"
},
{
"input": "iii iiiiii\n7\niii iiiiiiiiii\niiiiiiiiii iiii\niiii i\niiiiii iiiiiiii\niiiiiiii iiiiiiiii\ni iiiii\niiiii ii",
"output": "iii iiiiii\niiiiiiiiii iiiiii\niiii iiiiii\ni iiiiii\ni iiiiiiii\ni iiiiiiiii\niiiii iiiiiiiii\nii iiiiiiiii"
},
{
"input": "bwyplnjn zkms\n26\nzkms nzmcsytxh\nnzmcsytxh yujsb\nbwyplnjn gtbzhudpb\ngtbzhudpb hpk\nyujsb xvy\nhpk wrwnfokml\nwrwnfokml ndouuikw\nndouuikw ucgrja\nucgrja tgfmpldz\nxvy nycrfphn\nnycrfphn quvs\nquvs htdy\nhtdy k\ntgfmpldz xtdpkxm\nxtdpkxm suwqxs\nk fv\nsuwqxs qckllwy\nqckllwy diun\nfv lefa\nlefa gdoqjysx\ndiun dhpz\ngdoqjysx bdmqdyt\ndhpz dgz\ndgz v\nbdmqdyt aswy\naswy ydkayhlrnm",
"output": "bwyplnjn zkms\nbwyplnjn nzmcsytxh\nbwyplnjn yujsb\ngtbzhudpb yujsb\nhpk yujsb\nhpk xvy\nwrwnfokml xvy\nndouuikw xvy\nucgrja xvy\ntgfmpldz xvy\ntgfmpldz nycrfphn\ntgfmpldz quvs\ntgfmpldz htdy\ntgfmpldz k\nxtdpkxm k\nsuwqxs k\nsuwqxs fv\nqckllwy fv\ndiun fv\ndiun lefa\ndiun gdoqjysx\ndhpz gdoqjysx\ndhpz bdmqdyt\ndgz bdmqdyt\nv bdmqdyt\nv aswy\nv ydkayhlrnm"
},
{
"input": "wxz hbeqwqp\n7\nhbeqwqp cpieghnszh\ncpieghnszh tlqrpd\ntlqrpd ttwrtio\nttwrtio xapvds\nxapvds zk\nwxz yryk\nzk b",
"output": "wxz hbeqwqp\nwxz cpieghnszh\nwxz tlqrpd\nwxz ttwrtio\nwxz xapvds\nwxz zk\nyryk zk\nyryk b"
},
{
"input": "wced gnsgv\n23\ngnsgv japawpaf\njapawpaf nnvpeu\nnnvpeu a\na ddupputljq\nddupputljq qyhnvbh\nqyhnvbh pqwijl\nwced khuvs\nkhuvs bjkh\npqwijl ysacmboc\nbjkh srf\nsrf jknoz\njknoz hodf\nysacmboc xqtkoyh\nhodf rfp\nxqtkoyh bivgnwqvoe\nbivgnwqvoe nknf\nnknf wuig\nrfp e\ne bqqknq\nwuig sznhhhu\nbqqknq dhrtdld\ndhrtdld n\nsznhhhu bguylf",
"output": "wced gnsgv\nwced japawpaf\nwced nnvpeu\nwced a\nwced ddupputljq\nwced qyhnvbh\nwced pqwijl\nkhuvs pqwijl\nbjkh pqwijl\nbjkh ysacmboc\nsrf ysacmboc\njknoz ysacmboc\nhodf ysacmboc\nhodf xqtkoyh\nrfp xqtkoyh\nrfp bivgnwqvoe\nrfp nknf\nrfp wuig\ne wuig\nbqqknq wuig\nbqqknq sznhhhu\ndhrtdld sznhhhu\nn sznhhhu\nn bguylf"
},
{
"input": "qqqqqqqqqq qqqqqqqq\n3\nqqqqqqqq qqqqqqqqq\nqqqqqqqqq qqqqq\nqqqqq q",
"output": "qqqqqqqqqq qqqqqqqq\nqqqqqqqqqq qqqqqqqqq\nqqqqqqqqqq qqqqq\nqqqqqqqqqq q"
},
{
"input": "wwwww w\n8\nwwwww wwwwwwww\nwwwwwwww wwwwwwwww\nwwwwwwwww wwwwwwwwww\nw www\nwwwwwwwwww wwww\nwwww ww\nwww wwwwww\nwwwwww wwwwwww",
"output": "wwwww w\nwwwwwwww w\nwwwwwwwww w\nwwwwwwwwww w\nwwwwwwwwww www\nwwww www\nww www\nww wwwwww\nww wwwwwww"
},
{
"input": "k d\n17\nk l\nd v\nv z\nl r\nz i\nr s\ns p\np w\nw j\nj h\ni c\nh m\nm q\nc o\no g\nq x\nx n",
"output": "k d\nl d\nl v\nl z\nr z\nr i\ns i\np i\nw i\nj i\nh i\nh c\nm c\nq c\nq o\nq g\nx g\nn g"
}
] | 124 | 6,963,200 | 3 | 1,965 |
|
770 | Maximize Sum of Digits | [
"*special",
"implementation",
"math"
] | null | null | Anton has the integer *x*. He is interested what positive integer, which doesn't exceed *x*, has the maximum sum of digits.
Your task is to help Anton and to find the integer that interests him. If there are several such integers, determine the biggest of them. | The first line contains the positive integer *x* (1<=β€<=*x*<=β€<=1018) β the integer which Anton has. | Print the positive integer which doesn't exceed *x* and has the maximum sum of digits. If there are several such integers, print the biggest of them. Printed integer must not contain leading zeros. | [
"100\n",
"48\n",
"521\n"
] | [
"99\n",
"48\n",
"499\n"
] | none | [
{
"input": "100",
"output": "99"
},
{
"input": "48",
"output": "48"
},
{
"input": "521",
"output": "499"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "3",
"output": "3"
},
{
"input": "39188",
"output": "38999"
},
{
"input": "5",
"output": "5"
},
{
"input": "6",
"output": "6"
},
{
"input": "7",
"output": "7"
},
{
"input": "8",
"output": "8"
},
{
"input": "9",
"output": "9"
},
{
"input": "10",
"output": "9"
},
{
"input": "59999154",
"output": "59998999"
},
{
"input": "1000",
"output": "999"
},
{
"input": "10000",
"output": "9999"
},
{
"input": "100000",
"output": "99999"
},
{
"input": "1000000",
"output": "999999"
},
{
"input": "10000000",
"output": "9999999"
},
{
"input": "100000000",
"output": "99999999"
},
{
"input": "1000000000",
"output": "999999999"
},
{
"input": "10000000000",
"output": "9999999999"
},
{
"input": "100000000000",
"output": "99999999999"
},
{
"input": "1000000000000",
"output": "999999999999"
},
{
"input": "10000000000000",
"output": "9999999999999"
},
{
"input": "100000000000000",
"output": "99999999999999"
},
{
"input": "1000000000000000",
"output": "999999999999999"
},
{
"input": "10000000000000000",
"output": "9999999999999999"
},
{
"input": "100000000000000000",
"output": "99999999999999999"
},
{
"input": "1000000000000000000",
"output": "999999999999999999"
},
{
"input": "999999990",
"output": "999999989"
},
{
"input": "666666899789879",
"output": "599999999999999"
},
{
"input": "65499992294999000",
"output": "59999999999999999"
},
{
"input": "9879100000000099",
"output": "8999999999999999"
},
{
"input": "9991919190909919",
"output": "9989999999999999"
},
{
"input": "978916546899999999",
"output": "899999999999999999"
},
{
"input": "5684945999999999",
"output": "4999999999999999"
},
{
"input": "999999999999999999",
"output": "999999999999999999"
},
{
"input": "999999999999990999",
"output": "999999999999989999"
},
{
"input": "999999999999999990",
"output": "999999999999999989"
},
{
"input": "909999999999999999",
"output": "899999999999999999"
},
{
"input": "199999999999999999",
"output": "199999999999999999"
},
{
"input": "299999999999999999",
"output": "299999999999999999"
},
{
"input": "999999990009999999",
"output": "999999989999999999"
},
{
"input": "999000000001999999",
"output": "998999999999999999"
},
{
"input": "999999999991",
"output": "999999999989"
},
{
"input": "999999999992",
"output": "999999999989"
},
{
"input": "79320",
"output": "78999"
},
{
"input": "99004",
"output": "98999"
},
{
"input": "99088",
"output": "98999"
},
{
"input": "99737",
"output": "98999"
},
{
"input": "29652",
"output": "28999"
},
{
"input": "59195",
"output": "58999"
},
{
"input": "19930",
"output": "19899"
},
{
"input": "49533",
"output": "48999"
},
{
"input": "69291",
"output": "68999"
},
{
"input": "59452",
"output": "58999"
},
{
"input": "11",
"output": "9"
},
{
"input": "110",
"output": "99"
},
{
"input": "111",
"output": "99"
},
{
"input": "119",
"output": "99"
},
{
"input": "118",
"output": "99"
},
{
"input": "1100",
"output": "999"
},
{
"input": "1199",
"output": "999"
},
{
"input": "1109",
"output": "999"
},
{
"input": "1190",
"output": "999"
},
{
"input": "12",
"output": "9"
},
{
"input": "120",
"output": "99"
},
{
"input": "121",
"output": "99"
},
{
"input": "129",
"output": "99"
},
{
"input": "128",
"output": "99"
},
{
"input": "1200",
"output": "999"
},
{
"input": "1299",
"output": "999"
},
{
"input": "1209",
"output": "999"
},
{
"input": "1290",
"output": "999"
},
{
"input": "13",
"output": "9"
},
{
"input": "130",
"output": "99"
},
{
"input": "131",
"output": "99"
},
{
"input": "139",
"output": "99"
},
{
"input": "138",
"output": "99"
},
{
"input": "1300",
"output": "999"
},
{
"input": "1399",
"output": "999"
},
{
"input": "1309",
"output": "999"
},
{
"input": "1390",
"output": "999"
},
{
"input": "14",
"output": "9"
},
{
"input": "140",
"output": "99"
},
{
"input": "141",
"output": "99"
},
{
"input": "149",
"output": "99"
},
{
"input": "148",
"output": "99"
},
{
"input": "1400",
"output": "999"
},
{
"input": "1499",
"output": "999"
},
{
"input": "1409",
"output": "999"
},
{
"input": "1490",
"output": "999"
},
{
"input": "15",
"output": "9"
},
{
"input": "150",
"output": "99"
},
{
"input": "151",
"output": "99"
},
{
"input": "159",
"output": "99"
},
{
"input": "158",
"output": "99"
},
{
"input": "1500",
"output": "999"
},
{
"input": "1599",
"output": "999"
},
{
"input": "1509",
"output": "999"
},
{
"input": "1590",
"output": "999"
},
{
"input": "16",
"output": "9"
},
{
"input": "160",
"output": "99"
},
{
"input": "161",
"output": "99"
},
{
"input": "169",
"output": "99"
},
{
"input": "168",
"output": "99"
},
{
"input": "1600",
"output": "999"
},
{
"input": "1699",
"output": "999"
},
{
"input": "1609",
"output": "999"
},
{
"input": "1690",
"output": "999"
},
{
"input": "17",
"output": "9"
},
{
"input": "170",
"output": "99"
},
{
"input": "171",
"output": "99"
},
{
"input": "179",
"output": "99"
},
{
"input": "178",
"output": "99"
},
{
"input": "1700",
"output": "999"
},
{
"input": "1799",
"output": "999"
},
{
"input": "1709",
"output": "999"
},
{
"input": "1790",
"output": "999"
},
{
"input": "18",
"output": "18"
},
{
"input": "180",
"output": "99"
},
{
"input": "181",
"output": "99"
},
{
"input": "189",
"output": "189"
},
{
"input": "188",
"output": "99"
},
{
"input": "1800",
"output": "999"
},
{
"input": "1899",
"output": "1899"
},
{
"input": "1809",
"output": "999"
},
{
"input": "1890",
"output": "999"
},
{
"input": "19",
"output": "19"
},
{
"input": "190",
"output": "189"
},
{
"input": "191",
"output": "189"
},
{
"input": "199",
"output": "199"
},
{
"input": "198",
"output": "198"
},
{
"input": "1900",
"output": "1899"
},
{
"input": "1999",
"output": "1999"
},
{
"input": "1909",
"output": "1899"
},
{
"input": "1990",
"output": "1989"
},
{
"input": "20",
"output": "19"
},
{
"input": "200",
"output": "199"
},
{
"input": "201",
"output": "199"
},
{
"input": "209",
"output": "199"
},
{
"input": "208",
"output": "199"
},
{
"input": "2000",
"output": "1999"
},
{
"input": "2099",
"output": "1999"
},
{
"input": "2009",
"output": "1999"
},
{
"input": "2090",
"output": "1999"
},
{
"input": "21",
"output": "19"
},
{
"input": "210",
"output": "199"
},
{
"input": "211",
"output": "199"
},
{
"input": "219",
"output": "199"
},
{
"input": "218",
"output": "199"
},
{
"input": "2100",
"output": "1999"
},
{
"input": "2199",
"output": "1999"
},
{
"input": "2109",
"output": "1999"
},
{
"input": "2190",
"output": "1999"
},
{
"input": "22",
"output": "19"
},
{
"input": "220",
"output": "199"
},
{
"input": "221",
"output": "199"
},
{
"input": "229",
"output": "199"
},
{
"input": "228",
"output": "199"
},
{
"input": "2200",
"output": "1999"
},
{
"input": "2299",
"output": "1999"
},
{
"input": "2209",
"output": "1999"
},
{
"input": "2290",
"output": "1999"
},
{
"input": "23",
"output": "19"
},
{
"input": "230",
"output": "199"
},
{
"input": "231",
"output": "199"
},
{
"input": "239",
"output": "199"
},
{
"input": "238",
"output": "199"
},
{
"input": "2300",
"output": "1999"
},
{
"input": "2399",
"output": "1999"
},
{
"input": "2309",
"output": "1999"
},
{
"input": "2390",
"output": "1999"
},
{
"input": "24",
"output": "19"
},
{
"input": "240",
"output": "199"
},
{
"input": "241",
"output": "199"
},
{
"input": "249",
"output": "199"
},
{
"input": "248",
"output": "199"
},
{
"input": "2400",
"output": "1999"
},
{
"input": "2499",
"output": "1999"
},
{
"input": "2409",
"output": "1999"
},
{
"input": "2490",
"output": "1999"
},
{
"input": "25",
"output": "19"
},
{
"input": "250",
"output": "199"
},
{
"input": "251",
"output": "199"
},
{
"input": "259",
"output": "199"
},
{
"input": "258",
"output": "199"
},
{
"input": "2500",
"output": "1999"
},
{
"input": "2599",
"output": "1999"
},
{
"input": "2509",
"output": "1999"
},
{
"input": "2590",
"output": "1999"
},
{
"input": "26",
"output": "19"
},
{
"input": "260",
"output": "199"
},
{
"input": "261",
"output": "199"
},
{
"input": "269",
"output": "199"
},
{
"input": "268",
"output": "199"
},
{
"input": "2600",
"output": "1999"
},
{
"input": "2699",
"output": "1999"
},
{
"input": "2609",
"output": "1999"
},
{
"input": "2690",
"output": "1999"
},
{
"input": "27",
"output": "19"
},
{
"input": "270",
"output": "199"
},
{
"input": "271",
"output": "199"
},
{
"input": "279",
"output": "199"
},
{
"input": "278",
"output": "199"
},
{
"input": "2700",
"output": "1999"
},
{
"input": "2799",
"output": "1999"
},
{
"input": "2709",
"output": "1999"
},
{
"input": "2790",
"output": "1999"
},
{
"input": "28",
"output": "28"
},
{
"input": "280",
"output": "199"
},
{
"input": "281",
"output": "199"
},
{
"input": "289",
"output": "289"
},
{
"input": "288",
"output": "199"
},
{
"input": "2800",
"output": "1999"
},
{
"input": "2899",
"output": "2899"
},
{
"input": "2809",
"output": "1999"
},
{
"input": "2890",
"output": "1999"
},
{
"input": "29",
"output": "29"
},
{
"input": "290",
"output": "289"
},
{
"input": "291",
"output": "289"
},
{
"input": "299",
"output": "299"
},
{
"input": "298",
"output": "298"
},
{
"input": "2900",
"output": "2899"
},
{
"input": "2999",
"output": "2999"
},
{
"input": "2909",
"output": "2899"
},
{
"input": "2990",
"output": "2989"
},
{
"input": "999",
"output": "999"
},
{
"input": "999",
"output": "999"
},
{
"input": "890",
"output": "889"
},
{
"input": "995",
"output": "989"
},
{
"input": "999",
"output": "999"
},
{
"input": "989",
"output": "989"
},
{
"input": "999",
"output": "999"
},
{
"input": "999",
"output": "999"
},
{
"input": "991",
"output": "989"
},
{
"input": "999",
"output": "999"
},
{
"input": "9929",
"output": "9899"
},
{
"input": "4999",
"output": "4999"
},
{
"input": "9690",
"output": "8999"
},
{
"input": "8990",
"output": "8989"
},
{
"input": "9982",
"output": "9899"
},
{
"input": "9999",
"output": "9999"
},
{
"input": "1993",
"output": "1989"
},
{
"input": "9367",
"output": "8999"
},
{
"input": "8939",
"output": "8899"
},
{
"input": "9899",
"output": "9899"
},
{
"input": "99999",
"output": "99999"
},
{
"input": "93929",
"output": "89999"
},
{
"input": "99999",
"output": "99999"
},
{
"input": "38579",
"output": "29999"
},
{
"input": "79096",
"output": "78999"
},
{
"input": "72694",
"output": "69999"
},
{
"input": "99999",
"output": "99999"
},
{
"input": "99999",
"output": "99999"
},
{
"input": "99992",
"output": "99989"
},
{
"input": "27998",
"output": "19999"
},
{
"input": "460999",
"output": "399999"
},
{
"input": "999999",
"output": "999999"
},
{
"input": "999999",
"output": "999999"
},
{
"input": "998999",
"output": "998999"
},
{
"input": "999999",
"output": "999999"
},
{
"input": "999929",
"output": "999899"
},
{
"input": "999999",
"output": "999999"
},
{
"input": "999999",
"output": "999999"
},
{
"input": "979199",
"output": "899999"
},
{
"input": "999999",
"output": "999999"
},
{
"input": "9899999",
"output": "9899999"
},
{
"input": "9699959",
"output": "8999999"
},
{
"input": "9999999",
"output": "9999999"
},
{
"input": "9997099",
"output": "9989999"
},
{
"input": "8992091",
"output": "8989999"
},
{
"input": "9599295",
"output": "8999999"
},
{
"input": "2999902",
"output": "2999899"
},
{
"input": "9999953",
"output": "9999899"
},
{
"input": "9999999",
"output": "9999999"
},
{
"input": "9590999",
"output": "8999999"
}
] | 77 | 2,764,800 | -1 | 1,966 |
|
538 | Tourist's Notes | [
"binary search",
"brute force",
"greedy",
"implementation",
"math"
] | null | null | A tourist hiked along the mountain range. The hike lasted for *n* days, during each day the tourist noted height above the sea level. On the *i*-th day height was equal to some integer *h**i*. The tourist pick smooth enough route for his hike, meaning that the between any two consecutive days height changes by at most 1, i.e. for all *i*'s from 1 to *n*<=-<=1 the inequality |*h**i*<=-<=*h**i*<=+<=1|<=β€<=1 holds.
At the end of the route the tourist rafted down a mountain river and some notes in the journal were washed away. Moreover, the numbers in the notes could have been distorted. Now the tourist wonders what could be the maximum height during his hike. Help him restore the maximum possible value of the maximum height throughout the hike or determine that the notes were so much distorted that they do not represent any possible height values that meet limits |*h**i*<=-<=*h**i*<=+<=1|<=β€<=1. | The first line contains two space-separated numbers, *n* and *m* (1<=β€<=*n*<=β€<=108, 1<=β€<=*m*<=β€<=105)Β β the number of days of the hike and the number of notes left in the journal.
Next *m* lines contain two space-separated integers *d**i* and *h**d**i* (1<=β€<=*d**i*<=β€<=*n*, 0<=β€<=*h**d**i*<=β€<=108)Β β the number of the day when the *i*-th note was made and height on the *d**i*-th day. It is guaranteed that the notes are given in the chronological order, i.e. for all *i* from 1 to *m*<=-<=1 the following condition holds: *d**i*<=<<=*d**i*<=+<=1. | If the notes aren't contradictory, print a single integer β the maximum possible height value throughout the whole route.
If the notes do not correspond to any set of heights, print a single word 'IMPOSSIBLE' (without the quotes). | [
"8 2\n2 0\n7 0\n",
"8 3\n2 0\n7 0\n8 3\n"
] | [
"2\n",
"IMPOSSIBLE\n"
] | For the first sample, an example of a correct height sequence with a maximum of 2: (0,β0,β1,β2,β1,β1,β0,β1).
In the second sample the inequality between *h*<sub class="lower-index">7</sub> and *h*<sub class="lower-index">8</sub> does not hold, thus the information is inconsistent. | [
{
"input": "8 2\n2 0\n7 0",
"output": "2"
},
{
"input": "8 3\n2 0\n7 0\n8 3",
"output": "IMPOSSIBLE"
},
{
"input": "10 10\n1 0\n2 0\n3 0\n4 0\n5 1\n6 2\n7 3\n8 2\n9 3\n10 4",
"output": "4"
},
{
"input": "50 10\n1 42\n7 36\n16 40\n21 40\n26 39\n30 41\n32 41\n36 40\n44 37\n50 41",
"output": "42"
},
{
"input": "50 10\n5 17\n7 15\n10 4\n15 11\n18 13\n21 15\n31 5\n34 13\n40 15\n49 16",
"output": "IMPOSSIBLE"
},
{
"input": "100 50\n1 53\n3 51\n4 50\n6 48\n9 45\n12 48\n14 46\n16 48\n17 47\n19 49\n20 48\n22 46\n23 45\n24 44\n26 46\n27 47\n29 49\n32 52\n33 53\n35 55\n37 53\n40 50\n41 51\n43 53\n47 57\n50 60\n51 59\n52 60\n57 65\n59 63\n60 62\n61 61\n62 60\n64 62\n68 66\n70 64\n71 63\n73 65\n77 69\n79 67\n81 65\n83 63\n86 66\n88 68\n89 69\n91 67\n94 64\n95 63\n98 60\n100 58",
"output": "69"
},
{
"input": "10 1\n4 16160172",
"output": "16160178"
},
{
"input": "10000 2\n3270 897970\n8270 899508",
"output": "901239"
},
{
"input": "100000000 1\n9783835 100000000",
"output": "190216165"
},
{
"input": "100000000 1\n1 100000000",
"output": "199999999"
},
{
"input": "100000000 1\n100000000 100000000",
"output": "199999999"
},
{
"input": "100000000 2\n1 100000000\n100000000 100000000",
"output": "149999999"
},
{
"input": "100000000 2\n50000000 100000000\n50000001 100000000",
"output": "149999999"
},
{
"input": "99999999 20\n3 100000000\n14 100000000\n22 100000000\n24 100000000\n31 100000000\n41 100000000\n46 100000000\n84 100000000\n94 100000000\n98 100000000\n99999912 100000000\n99999915 100000000\n99999916 100000000\n99999923 100000000\n99999935 100000000\n99999937 100000000\n99999954 100000000\n99999955 100000000\n99999979 100000000\n99999982 100000000",
"output": "149999907"
},
{
"input": "8 3\n2 0\n7 3\n8 0",
"output": "IMPOSSIBLE"
},
{
"input": "10 1\n5 5",
"output": "10"
},
{
"input": "100 2\n1 100\n2 1",
"output": "IMPOSSIBLE"
},
{
"input": "1 1\n1 100",
"output": "100"
},
{
"input": "1 1\n1 10",
"output": "10"
},
{
"input": "1 1\n1 2",
"output": "2"
},
{
"input": "9 2\n1 8\n9 11",
"output": "13"
},
{
"input": "9 2\n1 1\n9 3",
"output": "6"
},
{
"input": "10000000 1\n10000000 0",
"output": "9999999"
},
{
"input": "3 2\n1 0\n3 0",
"output": "1"
},
{
"input": "100000000 2\n1 2\n2 1",
"output": "99999999"
},
{
"input": "1 1\n1 5",
"output": "5"
}
] | 62 | 5,734,400 | 0 | 1,969 |
|
985 | Switches and Lamps | [
"implementation"
] | null | null | You are given *n* switches and *m* lamps. The *i*-th switch turns on some subset of the lamps. This information is given as the matrix *a* consisting of *n* rows and *m* columns where *a**i*,<=*j*<==<=1 if the *i*-th switch turns on the *j*-th lamp and *a**i*,<=*j*<==<=0 if the *i*-th switch is not connected to the *j*-th lamp.
Initially all *m* lamps are turned off.
Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.
It is guaranteed that if you push all *n* switches then all *m* lamps will be turned on.
Your think that you have too many switches and you would like to ignore one of them.
Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other *n*<=-<=1 switches then all the *m* lamps will be turned on. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2000) β the number of the switches and the number of the lamps.
The following *n* lines contain *m* characters each. The character *a**i*,<=*j* is equal to '1' if the *i*-th switch turns on the *j*-th lamp and '0' otherwise.
It is guaranteed that if you press all *n* switches all *m* lamps will be turned on. | Print "YES" if there is a switch that if you will ignore it and press all the other *n*<=-<=1 switches then all *m* lamps will be turned on. Print "NO" if there is no such switch. | [
"4 5\n10101\n01000\n00111\n10000\n",
"4 5\n10100\n01000\n00110\n00101\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "4 5\n10101\n01000\n00111\n10000",
"output": "YES"
},
{
"input": "4 5\n10100\n01000\n00110\n00101",
"output": "NO"
},
{
"input": "1 5\n11111",
"output": "NO"
},
{
"input": "10 1\n1\n0\n0\n0\n0\n0\n0\n0\n0\n1",
"output": "YES"
},
{
"input": "1 1\n1",
"output": "NO"
},
{
"input": "3 4\n1010\n0100\n1101",
"output": "YES"
},
{
"input": "2 5\n10101\n11111",
"output": "YES"
},
{
"input": "5 5\n10000\n11000\n11100\n11110\n11111",
"output": "YES"
},
{
"input": "2 5\n10000\n11111",
"output": "YES"
},
{
"input": "4 5\n01000\n10100\n00010\n10101",
"output": "YES"
},
{
"input": "2 2\n10\n11",
"output": "YES"
},
{
"input": "2 5\n00100\n11111",
"output": "YES"
},
{
"input": "4 5\n00000\n11000\n00110\n00011",
"output": "YES"
},
{
"input": "4 3\n000\n010\n001\n100",
"output": "YES"
},
{
"input": "4 5\n10000\n10101\n01000\n00111",
"output": "YES"
},
{
"input": "4 5\n10000\n01000\n10101\n00111",
"output": "YES"
},
{
"input": "2 2\n01\n11",
"output": "YES"
},
{
"input": "3 3\n010\n101\n000",
"output": "YES"
},
{
"input": "2 2\n11\n00",
"output": "YES"
},
{
"input": "3 5\n10110\n11000\n00111",
"output": "YES"
},
{
"input": "3 8\n00111111\n01011100\n11000000",
"output": "YES"
},
{
"input": "4 6\n100000\n110000\n001100\n000011",
"output": "YES"
},
{
"input": "2 5\n11111\n00000",
"output": "YES"
},
{
"input": "2 3\n101\n111",
"output": "YES"
},
{
"input": "2 5\n01000\n11111",
"output": "YES"
},
{
"input": "2 2\n00\n11",
"output": "YES"
},
{
"input": "4 15\n111110100011010\n111111011010110\n101000001011001\n100110000111011",
"output": "YES"
},
{
"input": "2 3\n010\n111",
"output": "YES"
},
{
"input": "4 5\n10100\n11000\n00110\n00101",
"output": "YES"
},
{
"input": "4 4\n1111\n0000\n0000\n0000",
"output": "YES"
},
{
"input": "3 5\n11100\n00110\n00011",
"output": "YES"
},
{
"input": "2 1\n0\n1",
"output": "YES"
},
{
"input": "4 4\n1000\n1001\n0010\n0100",
"output": "YES"
},
{
"input": "3 5\n00110\n10011\n01100",
"output": "YES"
},
{
"input": "3 5\n10101\n00111\n01000",
"output": "NO"
},
{
"input": "4 5\n00101\n00011\n01000\n10010",
"output": "YES"
},
{
"input": "3 3\n100\n110\n111",
"output": "YES"
},
{
"input": "2 2\n11\n01",
"output": "YES"
},
{
"input": "3 3\n100\n100\n111",
"output": "YES"
},
{
"input": "4 2\n10\n01\n10\n01",
"output": "YES"
},
{
"input": "3 3\n111\n000\n000",
"output": "YES"
},
{
"input": "3 3\n010\n100\n011",
"output": "YES"
},
{
"input": "2 3\n111\n000",
"output": "YES"
},
{
"input": "3 4\n0001\n1101\n1010",
"output": "YES"
},
{
"input": "3 4\n1010\n0101\n1000",
"output": "YES"
},
{
"input": "3 4\n0001\n1101\n0110",
"output": "YES"
},
{
"input": "3 3\n111\n101\n001",
"output": "YES"
},
{
"input": "4 5\n10001\n10010\n01010\n00101",
"output": "YES"
},
{
"input": "3 3\n000\n000\n111",
"output": "YES"
},
{
"input": "2 3\n100\n111",
"output": "YES"
},
{
"input": "3 10\n1111011100\n0001100011\n1111010101",
"output": "YES"
},
{
"input": "3 4\n0110\n1010\n0101",
"output": "YES"
},
{
"input": "3 3\n100\n001\n011",
"output": "YES"
},
{
"input": "3 3\n100\n010\n001",
"output": "NO"
},
{
"input": "3 3\n010\n100\n001",
"output": "NO"
},
{
"input": "3 5\n10101\n01010\n01010",
"output": "YES"
},
{
"input": "2 6\n111111\n000000",
"output": "YES"
},
{
"input": "3 5\n00000\n10101\n01010",
"output": "YES"
},
{
"input": "4 6\n111000\n100100\n010010\n001001",
"output": "YES"
}
] | 109 | 22,323,200 | -1 | 1,971 |
|
463 | Gargari and Bishops | [
"greedy",
"hashing",
"implementation"
] | null | null | Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius.
He has a *n*<=Γ<=*n* chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number *x* written on it, if this cell is attacked by one of the bishops Gargari will get *x* dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money.
We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it). | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=2000). Each of the next *n* lines contains *n* integers *a**ij* (0<=β€<=*a**ij*<=β€<=109) β description of the chessboard. | On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: *x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=*n*), where *x**i* is the number of the row where the *i*-th bishop should be placed, *y**i* is the number of the column where the *i*-th bishop should be placed. Consider rows are numbered from 1 to *n* from top to bottom, and columns are numbered from 1 to *n* from left to right.
If there are several optimal solutions, you can print any of them. | [
"4\n1 1 1 1\n2 1 1 0\n1 1 1 0\n1 0 0 1\n"
] | [
"12\n2 2 3 2\n"
] | none | [
{
"input": "4\n1 1 1 1\n2 1 1 0\n1 1 1 0\n1 0 0 1",
"output": "12\n2 2 3 2"
},
{
"input": "10\n48 43 75 80 32 30 65 31 18 91\n99 5 12 43 26 90 54 91 4 88\n8 87 68 95 73 37 53 46 53 90\n50 1 85 24 32 16 5 48 98 74\n38 49 78 2 91 3 43 96 93 46\n35 100 84 2 94 56 90 98 54 43\n88 3 95 72 78 78 87 82 25 37\n8 15 85 85 68 27 40 10 22 84\n7 8 36 90 10 81 98 51 79 51\n93 66 53 39 89 30 16 27 63 93",
"output": "2242\n6 6 7 6"
},
{
"input": "10\n0 0 0 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\n0 0 0 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\n0 0 0 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\n0 0 0 0 0 0 0 0 0 0",
"output": "0\n1 1 1 2"
},
{
"input": "15\n2 6 9 4 8 9 10 10 3 8 8 4 4 8 7\n10 9 2 6 8 10 5 2 8 4 9 6 9 10 10\n3 1 5 1 6 5 1 6 4 4 3 3 9 8 10\n5 7 10 6 4 9 6 8 1 5 4 9 10 4 8\n9 6 10 5 8 6 9 9 3 4 4 7 6 2 4\n8 6 10 7 3 3 8 10 3 8 4 8 8 3 1\n7 3 6 8 8 5 5 8 3 7 2 6 3 9 7\n6 8 4 7 7 7 10 4 6 4 3 10 1 10 2\n1 6 7 8 3 4 2 8 1 7 4 4 4 9 5\n3 4 4 6 1 10 2 2 5 8 7 7 7 7 6\n10 9 3 6 8 6 1 9 5 4 7 10 7 1 8\n3 3 4 9 8 6 10 2 9 5 9 5 3 7 3\n1 8 1 3 4 8 10 4 8 4 7 5 4 6 7\n3 10 9 6 8 8 1 8 9 9 4 9 5 6 5\n7 6 3 9 9 8 6 10 3 6 4 2 10 9 7",
"output": "361\n7 9 9 8"
},
{
"input": "8\n3 6 9 2 2 1 4 2\n1 4 10 1 1 10 1 4\n3 8 9 1 8 4 4 4\n5 8 10 5 5 6 4 7\n3 2 10 6 5 3 8 5\n6 7 5 8 8 5 4 2\n4 4 3 1 8 8 5 4\n5 6 8 9 3 1 8 5",
"output": "159\n4 4 5 4"
},
{
"input": "13\n9 9 3 3 5 6 8 2 6 1 10 3 8\n10 4 9 2 10 3 5 10 10 7 10 7 3\n5 8 4 1 10 2 1 2 4 7 9 1 10\n6 3 10 10 10 1 3 10 4 4 2 10 4\n1 7 5 7 9 9 7 4 1 8 5 4 1\n10 10 9 2 2 6 4 1 5 5 1 9 4\n4 2 5 5 7 8 1 2 6 1 2 4 6\n5 1 10 8 1 1 9 1 2 10 6 7 2\n2 1 2 10 4 7 4 1 4 10 10 4 3\n7 7 5 1 2 1 1 4 8 2 4 8 2\n8 8 8 4 1 1 7 3 1 10 1 4 2\n4 5 1 10 8 8 8 4 10 9 4 10 4\n3 1 10 10 5 7 9 4 2 10 4 8 4",
"output": "280\n6 6 7 6"
},
{
"input": "9\n3 9 6 1 7 6 2 8 4\n5 4 1 1 7 2 7 4 10\n7 9 9 4 6 2 7 2 8\n5 7 7 4 9 5 9 1 3\n7 3 10 2 9 4 2 1 2\n5 8 7 4 6 6 2 2 3\n4 8 4 3 4 2 1 8 10\n5 8 2 8 4 4 7 5 4\n2 8 7 4 3 6 10 8 1",
"output": "181\n5 4 6 4"
}
] | 1,590 | 74,649,600 | 0 | 1,972 |
|
987 | High School: Become Human | [
"math"
] | null | null | Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids have to go to school to be able to solve creative tasks. Just like humans before.
It turns out that high school struggles are not gone. If someone is not like others, he is bullied. Vasya-8800 is an economy-class android which is produced by a little-known company. His design is not perfect, his characteristics also could be better. So he is bullied by other androids.
One of the popular pranks on Vasya is to force him to compare $x^y$ with $y^x$. Other androids can do it in milliseconds while Vasya's memory is too small to store such big numbers.
Please help Vasya! Write a fast program to compare $x^y$ with $y^x$ for Vasya, maybe then other androids will respect him. | On the only line of input there are two integers $x$ and $y$ ($1 \le x, y \le 10^{9}$). | If $x^y < y^x$, then print '<' (without quotes). If $x^y > y^x$, then print '>' (without quotes). If $x^y = y^x$, then print '=' (without quotes). | [
"5 8\n",
"10 3\n",
"6 6\n"
] | [
">\n",
"<\n",
"=\n"
] | In the first example $5^8 = 5 \cdot 5 \cdot 5 \cdot 5 \cdot 5 \cdot 5 \cdot 5 \cdot 5 = 390625$, and $8^5 = 8 \cdot 8 \cdot 8 \cdot 8 \cdot 8 = 32768$. So you should print '>'.
In the second example $10^3 = 1000 < 3^{10} = 59049$.
In the third example $6^6 = 46656 = 6^6$. | [
{
"input": "5 8",
"output": ">"
},
{
"input": "10 3",
"output": "<"
},
{
"input": "6 6",
"output": "="
},
{
"input": "14 1",
"output": ">"
},
{
"input": "2 4",
"output": "="
},
{
"input": "987654321 123456987",
"output": "<"
},
{
"input": "1 10",
"output": "<"
},
{
"input": "9 1",
"output": ">"
},
{
"input": "1 1",
"output": "="
},
{
"input": "2 2",
"output": "="
},
{
"input": "3 3",
"output": "="
},
{
"input": "4 4",
"output": "="
},
{
"input": "5 5",
"output": "="
},
{
"input": "2 3",
"output": "<"
},
{
"input": "2 5",
"output": ">"
},
{
"input": "3 2",
"output": ">"
},
{
"input": "3 4",
"output": ">"
},
{
"input": "3 5",
"output": ">"
},
{
"input": "4 2",
"output": "="
},
{
"input": "4 3",
"output": "<"
},
{
"input": "4 5",
"output": ">"
},
{
"input": "5 2",
"output": "<"
},
{
"input": "5 3",
"output": "<"
},
{
"input": "5 4",
"output": "<"
},
{
"input": "100 101",
"output": ">"
},
{
"input": "876543 372647",
"output": "<"
},
{
"input": "1000000000 999999999",
"output": "<"
},
{
"input": "1000000000 2",
"output": "<"
},
{
"input": "1000000000 1",
"output": ">"
},
{
"input": "987654321 123456789",
"output": "<"
},
{
"input": "1000000000 1000000000",
"output": "="
},
{
"input": "4359435 4396510",
"output": ">"
},
{
"input": "25936809 25936809",
"output": "="
},
{
"input": "53602896 3",
"output": "<"
},
{
"input": "13208659 1",
"output": ">"
},
{
"input": "620537015 620537016",
"output": ">"
},
{
"input": "56498103 56498102",
"output": "<"
},
{
"input": "4 1000000000",
"output": ">"
},
{
"input": "11 15",
"output": ">"
},
{
"input": "2 6",
"output": ">"
},
{
"input": "1 100",
"output": "<"
},
{
"input": "3 9",
"output": ">"
},
{
"input": "5 25",
"output": ">"
},
{
"input": "17 18",
"output": ">"
},
{
"input": "100 99",
"output": "<"
},
{
"input": "10000035 1000432",
"output": "<"
},
{
"input": "27 3",
"output": "<"
},
{
"input": "15657413 15657414",
"output": ">"
}
] | 156 | 0 | 3 | 1,974 |
|
451 | Sort the Array | [
"implementation",
"sortings"
] | null | null | Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array *a* consisting of *n* distinct integers.
Unfortunately, the size of *a* is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array *a* (in increasing order) by reversing exactly one segment of *a*? See definitions of segment and reversing in the notes. | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=105) β the size of array *a*.
The second line contains *n* distinct space-separated integers: *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=β€<=*a*[*i*]<=β€<=109). | Print "yes" or "no" (without quotes), depending on the answer.
If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these indices, print any of them. | [
"3\n3 2 1\n",
"4\n2 1 3 4\n",
"4\n3 1 2 4\n",
"2\n1 2\n"
] | [
"yes\n1 3\n",
"yes\n1 2\n",
"no\n",
"yes\n1 1\n"
] | Sample 1. You can reverse the entire array to get [1,β2,β3], which is sorted.
Sample 3. No segment can be reversed such that the array will be sorted.
Definitions
A segment [*l*,β*r*] of array *a* is the sequence *a*[*l*],β*a*[*l*β+β1],β...,β*a*[*r*].
If you have an array *a* of size *n* and you reverse its segment [*l*,β*r*], the array will become:
*a*[1],β*a*[2],β...,β*a*[*l*β-β2],β*a*[*l*β-β1],β*a*[*r*],β*a*[*r*β-β1],β...,β*a*[*l*β+β1],β*a*[*l*],β*a*[*r*β+β1],β*a*[*r*β+β2],β...,β*a*[*n*β-β1],β*a*[*n*]. | [
{
"input": "3\n3 2 1",
"output": "yes\n1 3"
},
{
"input": "4\n2 1 3 4",
"output": "yes\n1 2"
},
{
"input": "4\n3 1 2 4",
"output": "no"
},
{
"input": "2\n1 2",
"output": "yes\n1 1"
},
{
"input": "2\n58 4",
"output": "yes\n1 2"
},
{
"input": "5\n69 37 27 4 2",
"output": "yes\n1 5"
},
{
"input": "9\n6 78 63 59 28 24 8 96 99",
"output": "yes\n2 7"
},
{
"input": "6\n19517752 43452931 112792556 68417469 779722934 921694415",
"output": "yes\n3 4"
},
{
"input": "6\n169793171 335736854 449917902 513287332 811627074 938727967",
"output": "yes\n1 1"
},
{
"input": "6\n509329 173849943 297546987 591032670 796346199 914588283",
"output": "yes\n1 1"
},
{
"input": "25\n46 45 37 35 26 25 21 19 11 3 1 51 54 55 57 58 59 62 66 67 76 85 88 96 100",
"output": "yes\n1 11"
},
{
"input": "46\n10 12 17 19 20 21 22 24 25 26 27 28 29 30 32 37 42 43 47 48 50 51 52 56 87 86 81 79 74 71 69 67 66 65 60 59 57 89 91 92 94 96 97 98 99 100",
"output": "yes\n25 37"
},
{
"input": "96\n1 2 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 68 69 70 71 72 73 74 75 76 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100",
"output": "yes\n3 22"
},
{
"input": "2\n404928771 698395106",
"output": "yes\n1 1"
},
{
"input": "2\n699573624 308238132",
"output": "yes\n1 2"
},
{
"input": "5\n75531609 242194958 437796493 433259361 942142185",
"output": "yes\n3 4"
},
{
"input": "5\n226959376 840957605 833410429 273566427 872976052",
"output": "yes\n2 4"
},
{
"input": "5\n373362086 994096202 767275079 734424844 515504383",
"output": "yes\n2 5"
},
{
"input": "5\n866379155 593548704 259097686 216134784 879911740",
"output": "yes\n1 4"
},
{
"input": "5\n738083041 719956102 420866851 307749161 257917459",
"output": "yes\n1 5"
},
{
"input": "5\n90786760 107075352 139104198 424911569 858427981",
"output": "yes\n1 1"
},
{
"input": "6\n41533825 525419745 636375901 636653266 879043107 967434399",
"output": "yes\n1 1"
},
{
"input": "40\n22993199 75843013 76710455 99749069 105296587 122559115 125881005 153961749 163646706 175409222 185819807 214465092 264449243 278246513 295514446 322935239 370349154 375773209 390474983 775646826 767329655 740310077 718820037 708508595 693119912 680958422 669537382 629123011 607511013 546574974 546572137 511951383 506996390 493995578 458256840 815612821 881161983 901337648 962275390 986568907",
"output": "yes\n20 35"
},
{
"input": "40\n3284161 23121669 24630274 33434127 178753820 231503277 271972002 272578266 346450638 355655265 372217434 376132047 386622863 387235708 389799554 427160037 466577363 491873718 492746058 502535866 535768673 551570285 557477055 583643014 586216753 588981593 592960633 605923775 611051145 643142759 632768011 634888864 736715552 750574599 867737742 924365786 927179496 934453020 954090860 977765165",
"output": "no"
},
{
"input": "40\n42131757 49645896 49957344 78716964 120937785 129116222 172128600 211446903 247833196 779340466 717548386 709969818 696716905 636153997 635635467 614115746 609201167 533608141 521874836 273044950 291514539 394083281 399369419 448830087 485128983 487192341 488673105 497678164 501864738 265305156 799595875 831638598 835155840 845617770 847736630 851436542 879757553 885618675 964068808 969215471",
"output": "no"
},
{
"input": "40\n25722567 28250400 47661056 108729970 119887370 142272261 145287693 178946020 182917658 187405805 209478929 278713296 312035195 393514697 403876943 410188367 413061616 420619615 477231590 511200584 560288373 571690007 603093961 615463729 631624043 723138759 726089658 728151980 756393077 785590533 809755752 823601179 828357990 866942019 869575503 877310377 881382070 901314141 929048602 947139655",
"output": "yes\n1 1"
},
{
"input": "40\n17927221 33153935 60257083 110553879 114654567 119809916 163899753 167741765 182812464 188486743 220036903 220127072 227545828 229552200 244963635 248298934 299478582 354141058 371400641 430054473 452548736 458695269 466968129 469000714 478004472 478693873 509342093 750631027 609759323 669427158 688490225 690701652 696893030 704668825 749028408 557906039 545356441 926901326 955586118 972642992",
"output": "no"
},
{
"input": "4\n1 4 2 3",
"output": "no"
},
{
"input": "6\n1 2 5 4 3 6",
"output": "yes\n3 5"
},
{
"input": "1\n1",
"output": "yes\n1 1"
},
{
"input": "6\n1 5 3 4 2 6",
"output": "no"
},
{
"input": "4\n3 4 1 2",
"output": "no"
},
{
"input": "5\n2 5 4 3 1",
"output": "no"
},
{
"input": "4\n2 1 4 3",
"output": "no"
},
{
"input": "6\n2 1 4 3 5 6",
"output": "no"
}
] | 124 | 7,782,400 | 3 | 1,979 |
|
492 | Vanya and Cubes | [
"implementation"
] | null | null | Vanya got *n* cubes. He decided to build a pyramid from them. Vanya wants to build the pyramid as follows: the top level of the pyramid must consist of 1 cube, the second level must consist of 1<=+<=2<==<=3 cubes, the third level must have 1<=+<=2<=+<=3<==<=6 cubes, and so on. Thus, the *i*-th level of the pyramid must have 1<=+<=2<=+<=...<=+<=(*i*<=-<=1)<=+<=*i* cubes.
Vanya wants to know what is the maximum height of the pyramid that he can make using the given cubes. | The first line contains integer *n* (1<=β€<=*n*<=β€<=104) β the number of cubes given to Vanya. | Print the maximum possible height of the pyramid in the single line. | [
"1\n",
"25\n"
] | [
"1\n",
"4\n"
] | Illustration to the second sample: | [
{
"input": "1",
"output": "1"
},
{
"input": "25",
"output": "4"
},
{
"input": "2",
"output": "1"
},
{
"input": "4115",
"output": "28"
},
{
"input": "9894",
"output": "38"
},
{
"input": "7969",
"output": "35"
},
{
"input": "6560",
"output": "33"
},
{
"input": "4",
"output": "2"
},
{
"input": "3",
"output": "1"
},
{
"input": "5",
"output": "2"
},
{
"input": "19",
"output": "3"
},
{
"input": "20",
"output": "4"
},
{
"input": "9880",
"output": "38"
},
{
"input": "9879",
"output": "37"
},
{
"input": "7770",
"output": "35"
},
{
"input": "7769",
"output": "34"
},
{
"input": "2925",
"output": "25"
},
{
"input": "220",
"output": "10"
},
{
"input": "219",
"output": "9"
},
{
"input": "3046",
"output": "25"
},
{
"input": "7590",
"output": "34"
},
{
"input": "1014",
"output": "17"
},
{
"input": "7142",
"output": "34"
},
{
"input": "9999",
"output": "38"
},
{
"input": "10000",
"output": "38"
}
] | 62 | 0 | 3 | 1,988 |
|
980 | Posterized | [
"games",
"greedy"
] | null | null | Professor Ibrahim has prepared the final homework for his algorithmβs class. He asked his students to implement the Posterization Image Filter.
Their algorithm will be tested on an array of integers, where the $i$-th integer represents the color of the $i$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).
To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the groupβs key. In order to preserve image details, the size of a group must not be greater than $k$, and each color should belong to exactly one group.
Finally, the students will replace the color of each pixel in the array with that colorβs assigned group key.
To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $k$ to the right.
To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array. | The first line of input contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 256$), the number of pixels in the image, and the maximum size of a group, respectively.
The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($0 \leq p_i \leq 255$), where $p_i$ is the color of the $i$-th pixel. | Print $n$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter. | [
"4 3\n2 14 3 4\n",
"5 2\n0 2 1 255 254\n"
] | [
"0 12 3 3\n",
"0 1 1 254 254\n"
] | One possible way to group colors and assign keys for the first sample:
Color $2$ belongs to the group $[0,2]$, with group key $0$.
Color $14$ belongs to the group $[12,14]$, with group key $12$.
Colors $3$ and $4$ belong to group $[3, 5]$, with group key $3$.
Other groups won't affect the result so they are not listed here. | [
{
"input": "4 3\n2 14 3 4",
"output": "0 12 3 3"
},
{
"input": "5 2\n0 2 1 255 254",
"output": "0 1 1 254 254"
},
{
"input": "10 3\n112 184 161 156 118 231 191 128 91 229",
"output": "110 182 159 154 116 229 189 126 89 229"
},
{
"input": "9 3\n174 149 118 124 166 146 219 233 107",
"output": "172 147 116 122 164 144 217 231 105"
},
{
"input": "8 4\n180 195 13 195 61 24 132 160",
"output": "177 192 10 192 58 21 129 157"
},
{
"input": "1 4\n51",
"output": "48"
},
{
"input": "2 4\n218 213",
"output": "215 210"
},
{
"input": "3 3\n212 167 3",
"output": "210 165 1"
},
{
"input": "100 7\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38",
"output": "33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33"
},
{
"input": "4 2\n122 108 224 154",
"output": "121 107 223 153"
}
] | 61 | 7,065,600 | 0 | 1,994 |
|
864 | Polycarp and Letters | [
"brute force",
"implementation",
"strings"
] | null | null | Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string *s* consisting only of lowercase and uppercase Latin letters.
Let *A* be a set of positions in the string. Let's call it pretty if following conditions are met:
- letters on positions from *A* in the string are all distinct and lowercase; - there are no uppercase letters in the string which are situated between positions from *A* (i.e. there is no such *j* that *s*[*j*] is an uppercase letter, and *a*1<=<<=*j*<=<<=*a*2 for some *a*1 and *a*2 from *A*).
Write a program that will determine the maximum number of elements in a pretty set of positions. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=200) β length of string *s*.
The second line contains a string *s* consisting of lowercase and uppercase Latin letters. | Print maximum number of elements in pretty set of positions for string *s*. | [
"11\naaaaBaabAbA\n",
"12\nzACaAbbaazzC\n",
"3\nABC\n"
] | [
"2\n",
"3\n",
"0\n"
] | In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.
In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.
In the third example the given string *s* does not contain any lowercase letters, so the answer is 0. | [
{
"input": "11\naaaaBaabAbA",
"output": "2"
},
{
"input": "12\nzACaAbbaazzC",
"output": "3"
},
{
"input": "3\nABC",
"output": "0"
},
{
"input": "1\na",
"output": "1"
},
{
"input": "2\naz",
"output": "2"
},
{
"input": "200\nXbTJZqcbpYuZQEoUrbxlPXAPCtVLrRExpQzxzqzcqsqzsiisswqitswzCtJQxOavicSdBIodideVRKHPojCNHmbnrLgwJlwOpyrJJIhrUePszxSjJGeUgTtOfewPQnPVWhZAtogRPrJLwyShNQaeNsvrJwjuuBOMPCeSckBMISQzGngfOmeyfDObncyeNsihYVtQbSEh",
"output": "8"
},
{
"input": "2\nAZ",
"output": "0"
},
{
"input": "28\nAabcBabcCBNMaaaaabbbbbcccccc",
"output": "3"
},
{
"input": "200\nrsgraosldglhdoorwhkrsehjpuxrjuwgeanjgezhekprzarelduuaxdnspzjuooguuwnzkowkuhzduakdrzpnslauejhrrkalwpurpuuswdgeadlhjwzjgegwpknepazwwleulppwrlgrgedlwdzuodzropsrrkxusjnuzshdkjrxxpgzanzdrpnggdwxarpwohxdepJ",
"output": "17"
},
{
"input": "1\nk",
"output": "1"
},
{
"input": "1\nH",
"output": "0"
},
{
"input": "2\nzG",
"output": "1"
},
{
"input": "2\ngg",
"output": "1"
},
{
"input": "2\nai",
"output": "2"
},
{
"input": "20\npEjVrKWLIFCZjIHgggVU",
"output": "1"
},
{
"input": "20\niFSiiigiYFSKmDnMGcgM",
"output": "2"
},
{
"input": "20\nedxedxxxCQiIVmYEUtLi",
"output": "3"
},
{
"input": "20\nprnchweyabjvzkoqiltm",
"output": "20"
},
{
"input": "35\nQLDZNKFXKVSVLUVHRTDPQYMSTDXBELXBOTS",
"output": "0"
},
{
"input": "35\nbvZWiitgxodztelnYUyljYGnCoWluXTvBLp",
"output": "10"
},
{
"input": "35\nBTexnaeplecllxwlanarpcollawHLVMHIIF",
"output": "10"
},
{
"input": "35\nhhwxqysolegsthsvfcqiryenbujbrrScobu",
"output": "20"
},
{
"input": "26\npbgfqosklxjuzmdheyvawrictn",
"output": "26"
},
{
"input": "100\nchMRWwymTDuZDZuSTvUmmuxvSscnTasyjlwwodhzcoifeahnbmcifyeobbydwparebduoLDCgHlOsPtVRbYGGQXfnkdvrWKIwCRl",
"output": "20"
},
{
"input": "100\nhXYLXKUMBrGkjqQJTGbGWAfmztqqapdbjbhcualhypgnaieKXmhzGMnqXVlcPesskfaEVgvWQTTShRRnEtFahWDyuBzySMpugxCM",
"output": "19"
},
{
"input": "100\nucOgELrgjMrFOgtHzqgvUgtHngKJxdMFKBjfcCppciqmGZXXoiSZibgpadshyljqrwxbomzeutvnhTLGVckZUmyiFPLlwuLBFito",
"output": "23"
},
{
"input": "200\nWTCKAKLVGXSYFVMVJDUYERXNMVNTGWXUGRFCGMYXJQGLODYZTUIDENHYEGFKXFIEUILAMESAXAWZXVCZPJPEYUXBITHMTZOTMKWITGRSFHODKVJHPAHVVWTCTHIVAWAREQXWMPUWQSTPPJFHKGKELBTPUYDAVIUMGASPUEDIODRYXIWCORHOSLIBLOZUNJPHHMXEXOAY",
"output": "0"
},
{
"input": "200\neLCCuYMPPwQoNlCpPOtKWJaQJmWfHeZCKiMSpILHSKjFOYGpRMzMCfMXdDuQdBGNsCNrHIVJzEFfBZcNMwNcFjOFVJvEtUQmLbFNKVHgNDyFkFVQhUTUQDgXhMjJZgFSSiHhMKuTgZQYJqAqKBpHoHddddddddddddddddXSSYNKNnRrKuOjAVKZlRLzCjExPdHaDHBT",
"output": "1"
},
{
"input": "200\nitSYxgOLlwOoAkkkkkzzzzzzzzkzkzkzkkkkkzkzzkzUDJSKybRPBvaIDsNuWImPJvrHkKiMeYukWmtHtgZSyQsgYanZvXNbKXBlFLSUcqRnGWSriAvKxsTkDJfROqaKdzXhvJsPEDATueCraWOGEvRDWjPwXuiNpWsEnCuhDcKWOQxjBkdBqmFatWFkgKsbZuLtRGtY",
"output": "2"
},
{
"input": "200\noggqoqqogoqoggggoggqgooqggogogooogqqgggoqgggqoqogogggogggqgooqgqggqqqoqgqgoooqgqogqoggoqqgqoqgoooqoogooqoogqoqoqqgoqgoqgggogqqqoqoggoqoqqoqggqoggooqqqoqggoggqqqqqqqqqgogqgggggooogogqgggqogqgoqoqogoooq",
"output": "3"
},
{
"input": "200\nCtclUtUnmqFniaLqGRmMoUMeLyFfAgWxIZxdrBarcRQprSOGcdUYsmDbooSuOvBLgrYlgaIjJtFgcxJKHGkCXpYfVKmUbouuIqGstFrrwJzYQqjjqqppqqqqqpqqqjpjjpjqjXRYkfPhGAatOigFuItkKxkjCBLdiNMVGjmdWNMgOOvmaJEdGsWNoaERrINNKqKeQajv",
"output": "3"
},
{
"input": "200\nmeZNrhqtSTSmktGQnnNOTcnyAMTKSixxKQKiagrMqRYBqgbRlsbJhvtNeHVUuMCyZLCnsIixRYrYEAkfQOxSVqXkrPqeCZQksInzRsRKBgvIqlGVPxPQnypknSXjgMjsjElcqGsaJRbegJVAKtWcHoOnzHqzhoKReqBBsOhZYLaYJhmqOMQsizdCsQfjUDHcTtHoeYwu",
"output": "4"
},
{
"input": "200\nvFAYTHJLZaivWzSYmiuDBDUFACDSVbkImnVaXBpCgrbgmTfXKJfoglIkZxWPSeVSFPnHZDNUAqLyhjLXSuAqGLskBlDxjxGPJyGdwzlPfIekwsblIrkxzfhJeNoHywdfAGlJzqXOfQaKceSqViVFTRJEGfACnsFeSFpOYisIHJciqTMNAmgeXeublTvfWoPnddtvKIyF",
"output": "6"
},
{
"input": "200\ngnDdkqJjYvduVYDSsswZDvoCouyaYZTfhmpSakERWLhufZtthWsfbQdTGwhKYjEcrqWBOyxBbiFhdLlIjChLOPiOpYmcrJgDtXsJfmHtLrabyGKOfHQRukEtTzwoqBHfmyVXPebfcpGQacLkGWFwerszjdHpTBXGssYXmGHlcCBgBXyGJqxbVhvDffLyCrZnxonABEXV",
"output": "7"
},
{
"input": "200\nBmggKNRZBXPtJqlJaXLdKKQLDJvXpDuQGupiRQfDwCJCJvAlDDGpPZNOvXkrdKOFOEFBVfrsZjWyHPoKGzXmTAyPJGEmxCyCXpeAdTwbrMtWLmlmGNqxvuxmqpmtpuhrmxxtrquSLFYVlnSYgRJDYHWgHBbziBLZRwCIJNvbtsEdLLxmTbnjkoqSPAuzEeTYLlmejOUH",
"output": "9"
},
{
"input": "200\nMkuxcDWdcnqsrlTsejehQKrTwoOBRCUAywqSnZkDLRmVBDVoOqdZHbrInQQyeRFAjiYYmHGrBbWgWstCPfLPRdNVDXBdqFJsGQfSXbufsiogybEhKDlWfPazIuhpONwGzZWaQNwVnmhTqWdewaklgjwaumXYDGwjSeEcYXjkVtLiYSWULEnTFukIlWQGWsXwWRMJGTcI",
"output": "10"
},
{
"input": "200\nOgMBgYeuMJdjPtLybvwmGDrQEOhliaabEtwulzNEjsfnaznXUMoBbbxkLEwSQzcLrlJdjJCLGVNBxorghPxTYCoqniySJMcilpsqpBAbqdzqRUDVaYOgqGhGrxlIJkyYgkOdTUgRZwpgIkeZFXojLXpDilzirHVVadiHaMrxhzodzpdvhvrzdzxbhmhdpxqqpoDegfFQ",
"output": "11"
},
{
"input": "200\nOLaJOtwultZLiZPSYAVGIbYvbIuZkqFZXwfsqpsavCDmBMStAuUFLBVknWDXNzmiuUYIsUMGxtoadWlPYPqvqSvpYdOiJRxFzGGnnmstniltvitnrmyrblnqyruylummmlsqtqitlbulvtuitiqimuintbimqyurviuntqnnvslynlNYMpYVKYwKVTbIUVdlNGrcFZON",
"output": "12"
},
{
"input": "200\nGAcmlaqfjSAQLvXlkhxujXgSbxdFAwnoxDuldDvYmpUhTWJdcEQSdARLrozJzIgFVCkzPUztWIpaGfiKeqzoXinEjVuoKqyBHmtFjBWcRdBmyjviNlGAIkpikjAimmBgayfphrstfbjexjbttzfzfzaysxfyrjazfhtpghnbbeffjhxrjxpttesgzrnrfbgzzsRsCgmz",
"output": "15"
},
{
"input": "200\nYRvIopNqSTYDhViTqCLMwEbTTIdHkoeuBmAJWhgtOgVxlcHSsavDNzMfpwTghkBvYEtCYQxicLUxdgAcaCzOOgbQYsfnaTXFlFxbeEiGwdNvxwHzkTdKtWlqzalwniDDBDipkxfflpaqkfkgfezbkxdvzemlfohwtgytzzywmwhvzUgPlPdeAVqTPAUZbogQheRXetvT",
"output": "20"
},
{
"input": "200\nNcYVomemswLCUqVRSDKHCknlBmqeSWhVyRzQrnZaOANnTGqsRFMjpczllcEVebqpxdavzppvztxsnfmtcharzqlginndyjkawzurqkxJLXiXKNZTIIxhSQghDpjwzatEqnLMTLxwoEKpHytvWkKFDUcZjLShCiVdocxRvvJtbXHCDGpJvMwRKWLhcTFtswdLUHkbhfau",
"output": "25"
},
{
"input": "200\nDxNZuvkTkQEqdWIkLzcKAwfqvZQiptnTazydSCTIfGjDhLMrlPZiKEsqIdDhgKPAlEvXyzNwWtYorotgkcwydpabjqnzubaksdchucxtkmjzfretdmvlxgklyvicrtftvztsbiUaQorfNIYUOdwQDRsKpxLUiLknbLbinilpPXPTTwLAnXVpMHBaAcKWgDBeOFabPtXU",
"output": "26"
},
{
"input": "4\nabbc",
"output": "3"
},
{
"input": "3\naaa",
"output": "1"
},
{
"input": "3\naba",
"output": "2"
},
{
"input": "3\nabb",
"output": "2"
},
{
"input": "3\nbba",
"output": "2"
},
{
"input": "3\nAaa",
"output": "1"
},
{
"input": "3\nAba",
"output": "2"
},
{
"input": "3\naBa",
"output": "1"
},
{
"input": "3\naAa",
"output": "1"
},
{
"input": "3\naAb",
"output": "1"
},
{
"input": "3\nAaA",
"output": "1"
},
{
"input": "5\naBacd",
"output": "3"
},
{
"input": "5\naAabc",
"output": "3"
}
] | 124 | 6,656,000 | 3 | 1,996 |
|
977 | Two-gram | [
"implementation",
"strings"
] | null | null | Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" β three distinct two-grams.
You are given a string $s$ consisting of $n$ capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string $s$ = "BBAABBBA" the answer is two-gram "BB", which contained in $s$ three times. In other words, find any most frequent two-gram.
Note that occurrences of the two-gram can overlap with each other. | The first line of the input contains integer number $n$ ($2 \le n \le 100$) β the length of string $s$. The second line of the input contains the string $s$ consisting of $n$ capital Latin letters. | Print the only line containing exactly two capital Latin letters β any two-gram contained in the given string $s$ as a substring (i.e. two consecutive characters of the string) maximal number of times. | [
"7\nABACABA\n",
"5\nZZZAA\n"
] | [
"AB\n",
"ZZ\n"
] | In the first example "BA" is also valid answer.
In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times. | [
{
"input": "7\nABACABA",
"output": "AB"
},
{
"input": "5\nZZZAA",
"output": "ZZ"
},
{
"input": "26\nQWERTYUIOPASDFGHJKLZXCVBNM",
"output": "AS"
},
{
"input": "2\nQA",
"output": "QA"
},
{
"input": "2\nWW",
"output": "WW"
},
{
"input": "11\nGGRRAATTZZZ",
"output": "ZZ"
},
{
"input": "50\nNYQAHBYYOXLTRYQDMVENEMAQNBAKGLGQOLXNAIFNQTOCLNNQIA",
"output": "NQ"
},
{
"input": "100\nURXCAIZFIBNJTPCZHBQIBCILLPXZCFGMKKZMNPLCYGAVJVIBMCZEBSJWPSCPQDYCTTKPOKIJRSKIZPDGCHVOUTMPNECYORSFZFNC",
"output": "IB"
},
{
"input": "100\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
"output": "AA"
},
{
"input": "10\nSQSQSQSQTG",
"output": "SQ"
},
{
"input": "5\nAZAZA",
"output": "AZ"
},
{
"input": "15\nMIRZOYANOVECLOX",
"output": "AN"
},
{
"input": "9\nEGORLETOV",
"output": "EG"
},
{
"input": "8\nPUTINVOR",
"output": "IN"
},
{
"input": "7\nKADUROV",
"output": "AD"
},
{
"input": "6\nAZAZAZ",
"output": "AZ"
},
{
"input": "3\nLOL",
"output": "LO"
},
{
"input": "3\nKEK",
"output": "EK"
},
{
"input": "5\nFUFEL",
"output": "EL"
},
{
"input": "9\nMIKEPIDOR",
"output": "DO"
},
{
"input": "9\nAAAAAAAAA",
"output": "AA"
},
{
"input": "23\nAABBBAAACCCCCAAADDDDDDD",
"output": "DD"
}
] | 46 | 28,774,400 | 0 | 1,997 |
|
930 | Peculiar apple-tree | [
"dfs and similar",
"graphs",
"trees"
] | null | null | In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are *n* inflorescences, numbered from 1 to *n*. Inflorescence number 1 is situated near base of tree and any other inflorescence with number *i* (*i*<=><=1) is situated at the top of branch, which bottom is *p**i*-th inflorescence and *p**i*<=<<=*i*.
Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in *a*-th inflorescence gets to *p**a*-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time.
Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. | First line of input contains single integer number *n* (2<=β€<=*n*<=β€<=100<=000) Β β number of inflorescences.
Second line of input contains sequence of *n*<=-<=1 integer numbers *p*2,<=*p*3,<=...,<=*p**n* (1<=β€<=*p**i*<=<<=*i*), where *p**i* is number of inflorescence into which the apple from *i*-th inflorescence rolls down. | Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. | [
"3\n1 1\n",
"5\n1 2 2 2\n",
"18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4\n"
] | [
"1\n",
"3\n",
"4\n"
] | In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them.
In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it. | [
{
"input": "3\n1 1",
"output": "1"
},
{
"input": "5\n1 2 2 2",
"output": "3"
},
{
"input": "18\n1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4",
"output": "4"
},
{
"input": "2\n1",
"output": "2"
},
{
"input": "3\n1 2",
"output": "3"
},
{
"input": "20\n1 1 1 1 1 4 1 2 4 1 2 1 7 1 2 2 9 7 1",
"output": "2"
},
{
"input": "20\n1 2 1 2 2 1 2 4 1 6 2 2 4 3 2 6 2 5 9",
"output": "2"
},
{
"input": "20\n1 1 1 4 2 4 3 1 2 8 3 2 11 13 15 1 12 13 12",
"output": "4"
},
{
"input": "20\n1 2 2 4 3 5 5 6 6 9 11 9 9 12 13 10 15 13 15",
"output": "4"
},
{
"input": "20\n1 2 3 4 5 6 7 8 9 6 11 12 12 7 13 15 16 11 13",
"output": "8"
},
{
"input": "10\n1 1 1 2 1 3 4 2 1",
"output": "2"
},
{
"input": "30\n1 1 1 2 1 2 1 1 2 1 1 1 2 2 4 3 6 2 3 5 3 4 11 5 3 3 4 7 6",
"output": "4"
},
{
"input": "40\n1 1 1 1 1 1 1 1 1 3 4 3 3 1 3 6 7 4 5 2 4 3 9 1 4 2 5 3 5 9 5 9 10 12 3 7 2 11 1",
"output": "2"
},
{
"input": "50\n1 1 1 1 1 2 3 3 2 1 1 2 3 1 3 1 5 6 4 1 1 2 1 2 1 10 17 2 2 4 12 9 6 6 5 13 1 3 2 8 25 3 22 1 10 13 6 3 2",
"output": "4"
},
{
"input": "10\n1 1 1 1 2 1 3 4 3",
"output": "2"
},
{
"input": "30\n1 2 1 1 1 2 1 4 2 3 9 2 3 2 1 1 4 3 12 4 8 8 3 7 9 1 9 19 1",
"output": "2"
},
{
"input": "40\n1 1 1 2 3 1 2 1 3 7 1 3 4 3 2 3 4 1 2 2 4 1 7 4 1 3 2 1 4 5 3 10 14 11 10 13 8 7 4",
"output": "2"
},
{
"input": "50\n1 2 1 1 1 3 1 3 1 5 3 2 7 3 6 6 3 1 4 2 3 10 8 9 1 4 5 2 8 6 12 9 7 5 7 19 3 15 10 4 12 4 19 5 16 5 3 13 5",
"output": "2"
},
{
"input": "10\n1 1 1 2 3 2 1 2 3",
"output": "2"
},
{
"input": "30\n1 1 1 1 2 1 4 4 2 3 2 1 1 1 1 3 1 1 3 2 3 5 1 2 9 16 2 4 3",
"output": "2"
},
{
"input": "40\n1 1 1 2 1 2 1 2 4 8 1 7 1 6 2 8 2 12 4 11 5 5 15 3 12 11 22 11 13 13 24 6 10 15 3 6 7 1 2",
"output": "2"
},
{
"input": "50\n1 1 1 1 3 4 1 2 3 5 1 2 1 5 1 10 4 11 1 8 8 4 4 12 5 3 4 1 1 2 5 13 13 2 2 10 12 3 19 14 1 1 15 3 23 21 12 3 14",
"output": "4"
},
{
"input": "10\n1 1 1 1 2 4 1 1 3",
"output": "2"
},
{
"input": "30\n1 1 1 1 3 3 2 3 7 4 1 2 4 6 2 8 1 2 13 7 5 15 3 3 8 4 4 18 3",
"output": "2"
},
{
"input": "40\n1 1 1 2 2 1 1 4 6 4 7 7 7 4 4 8 10 7 5 1 5 13 7 8 2 11 18 2 1 20 7 3 12 16 2 22 4 22 14",
"output": "4"
},
{
"input": "50\n1 1 1 2 2 1 3 5 3 1 9 4 4 2 12 15 3 13 8 8 4 13 20 17 19 2 4 3 9 5 17 9 17 1 5 7 6 5 20 11 31 33 32 20 6 25 1 2 6",
"output": "4"
},
{
"input": "10\n1 1 1 3 3 5 6 8 3",
"output": "4"
},
{
"input": "30\n1 2 2 1 5 5 5 1 7 4 10 2 4 11 2 3 10 10 7 13 12 4 10 3 22 25 8 1 1",
"output": "6"
},
{
"input": "40\n1 2 2 2 2 4 2 2 6 9 3 9 9 9 3 5 7 7 2 17 4 4 8 8 25 18 12 27 8 19 26 15 33 26 33 9 24 4 27",
"output": "4"
},
{
"input": "50\n1 1 3 3 4 5 5 2 4 3 9 9 1 5 5 7 5 5 16 1 18 3 6 5 6 13 26 12 23 20 17 21 9 17 19 34 12 24 11 9 32 10 40 42 7 40 11 25 3",
"output": "6"
},
{
"input": "10\n1 2 1 2 5 5 6 6 6",
"output": "2"
},
{
"input": "30\n1 1 3 3 5 6 7 5 7 6 5 4 8 6 10 12 14 9 15 20 6 21 14 24 17 23 23 18 8",
"output": "2"
},
{
"input": "40\n1 2 2 3 1 2 5 6 4 8 11 12 9 5 12 7 4 16 16 15 6 22 17 24 10 8 22 4 27 9 19 23 16 18 28 22 5 35 19",
"output": "4"
},
{
"input": "50\n1 2 3 4 5 5 5 7 1 2 11 5 7 11 11 11 15 3 17 10 6 18 14 14 24 11 10 7 17 18 8 7 19 18 31 27 21 30 34 32 27 39 38 22 32 23 31 48 25",
"output": "2"
},
{
"input": "10\n1 2 2 4 5 5 6 4 7",
"output": "2"
},
{
"input": "30\n1 2 3 3 5 6 3 8 9 10 10 10 11 7 8 8 15 16 13 13 19 12 15 18 18 24 27 25 10",
"output": "6"
},
{
"input": "40\n1 2 3 4 5 6 6 8 7 10 11 3 12 11 15 12 17 15 10 20 16 20 12 20 15 21 20 26 29 23 29 30 23 24 35 33 25 32 36",
"output": "8"
},
{
"input": "50\n1 2 2 2 5 6 7 7 9 10 7 4 5 4 15 15 16 17 10 19 18 16 15 24 20 8 27 16 19 24 23 32 17 23 29 18 35 35 38 35 39 41 42 38 19 46 38 28 29",
"output": "6"
},
{
"input": "10\n1 2 3 4 5 5 5 7 9",
"output": "8"
},
{
"input": "30\n1 2 3 4 5 6 5 3 6 7 8 11 12 13 15 15 13 13 19 10 14 10 15 23 21 9 27 22 28",
"output": "4"
},
{
"input": "40\n1 2 2 3 3 6 5 5 9 7 8 11 13 7 10 10 16 14 18 20 11 19 23 18 20 21 25 16 29 25 27 31 26 34 33 23 36 33 32",
"output": "6"
},
{
"input": "50\n1 2 2 4 5 5 7 6 9 10 11 12 13 7 14 15 14 17 10 14 9 21 23 23 19 26 19 25 11 24 22 27 26 34 35 30 37 31 38 32 40 32 42 44 37 21 40 40 48",
"output": "10"
},
{
"input": "10\n1 2 3 4 3 6 6 6 7",
"output": "4"
},
{
"input": "30\n1 2 2 4 5 6 5 7 9 6 4 12 7 14 12 12 15 17 13 12 8 20 21 15 17 24 21 19 16",
"output": "4"
},
{
"input": "40\n1 2 3 4 4 6 6 4 9 9 10 12 10 12 12 16 8 13 18 14 17 20 21 23 25 22 25 26 29 26 27 27 33 31 33 34 36 29 34",
"output": "10"
},
{
"input": "50\n1 2 3 3 4 3 6 7 8 10 11 10 12 11 11 14 13 8 17 20 21 19 15 18 21 18 17 23 25 28 25 27 29 32 32 34 37 29 30 39 41 35 24 41 37 36 41 35 43",
"output": "10"
},
{
"input": "99\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",
"output": "1"
},
{
"input": "99\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98",
"output": "99"
},
{
"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",
"output": "2"
},
{
"input": "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99",
"output": "100"
}
] | 451 | 47,001,600 | -1 | 1,999 |
|
250 | Movie Critics | [
"greedy"
] | null | null | A film festival is coming up in the city N. The festival will last for exactly *n* days and each day will have a premiere of exactly one film. Each film has a genre β an integer from 1 to *k*.
On the *i*-th day the festival will show a movie of genre *a**i*. We know that a movie of each of *k* genres occurs in the festival programme at least once. In other words, each integer from 1 to *k* occurs in the sequence *a*1,<=*a*2,<=...,<=*a**n* at least once.
Valentine is a movie critic. He wants to watch some movies of the festival and then describe his impressions on his site.
As any creative person, Valentine is very susceptive. After he watched the movie of a certain genre, Valentine forms the mood he preserves until he watches the next movie. If the genre of the next movie is the same, it does not change Valentine's mood. If the genres are different, Valentine's mood changes according to the new genre and Valentine has a stress.
Valentine can't watch all *n* movies, so he decided to exclude from his to-watch list movies of one of the genres. In other words, Valentine is going to choose exactly one of the *k* genres and will skip all the movies of this genre. He is sure to visit other movies.
Valentine wants to choose such genre *x* (1<=β€<=*x*<=β€<=*k*), that the total number of after-movie stresses (after all movies of genre *x* are excluded) were minimum. | The first line of the input contains two integers *n* and *k* (2<=β€<=*k*<=β€<=*n*<=β€<=105), where *n* is the number of movies and *k* is the number of genres.
The second line of the input contains a sequence of *n* positive integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=*k*), where *a**i* is the genre of the *i*-th movie. It is guaranteed that each number from 1 to *k* occurs at least once in this sequence. | Print a single number β the number of the genre (from 1 to *k*) of the excluded films. If there are multiple answers, print the genre with the minimum number. | [
"10 3\n1 1 2 3 2 3 3 1 1 3\n",
"7 3\n3 1 3 2 3 1 2\n"
] | [
"3",
"1"
] | In the first sample if we exclude the movies of the 1st genre, the genres 2,β3,β2,β3,β3,β3 remain, that is 3 stresses; if we exclude the movies of the 2nd genre, the genres 1,β1,β3,β3,β3,β1,β1,β3 remain, that is 3 stresses; if we exclude the movies of the 3rd genre the genres 1,β1,β2,β2,β1,β1 remain, that is 2 stresses.
In the second sample whatever genre Valentine excludes, he will have exactly 3 stresses. | [
{
"input": "10 3\n1 1 2 3 2 3 3 1 1 3",
"output": "3"
},
{
"input": "7 3\n3 1 3 2 3 1 2",
"output": "1"
},
{
"input": "2 2\n1 2",
"output": "1"
},
{
"input": "10 2\n1 2 2 1 1 2 1 1 2 2",
"output": "1"
},
{
"input": "10 10\n5 7 8 2 4 10 1 3 9 6",
"output": "1"
},
{
"input": "100 10\n6 2 8 1 7 1 2 9 2 6 10 4 2 8 7 5 2 9 5 2 3 2 8 3 7 2 4 3 1 8 8 5 7 10 2 1 8 4 1 4 9 4 2 1 9 3 7 2 4 8 4 3 10 3 9 5 7 7 1 2 10 7 7 8 9 7 1 7 4 8 9 4 1 10 2 4 2 10 9 6 10 5 1 4 2 1 3 1 6 9 10 1 8 9 1 9 1 1 7 6",
"output": "1"
},
{
"input": "74 10\n10 5 4 7 1 9 3 5 10 7 1 4 8 8 4 1 3 9 3 3 10 6 10 4 2 8 9 7 3 2 5 3 6 7 10 4 4 7 8 2 3 10 5 10 5 10 7 9 9 6 1 10 8 9 7 8 9 10 3 6 10 9 9 5 10 6 4 3 5 3 6 8 9 3",
"output": "10"
},
{
"input": "113 3\n1 3 2 2 1 3 1 2 2 2 3 1 1 3 1 3 3 1 2 2 1 3 2 3 3 1 3 1 1 3 3 1 2 3 3 1 3 3 2 3 3 1 1 1 1 2 3 2 2 3 3 2 3 1 3 2 1 3 2 1 1 2 2 2 2 2 1 1 3 3 2 1 1 3 2 2 1 3 1 1 1 3 3 2 1 2 2 3 3 1 3 1 2 2 1 2 2 3 3 2 3 1 3 1 1 2 3 2 3 2 3 1 3",
"output": "3"
},
{
"input": "129 37\n15 22 28 15 30 16 11 20 4 26 14 37 29 17 15 15 14 18 1 9 16 22 13 32 5 18 25 11 6 33 33 22 17 9 1 5 27 27 37 4 6 1 3 18 11 33 6 17 2 24 23 1 1 37 35 21 21 17 18 6 31 17 29 18 10 10 30 31 8 3 25 20 34 11 12 12 24 13 5 27 20 31 28 3 36 2 4 13 14 9 33 25 4 26 36 6 2 7 15 9 20 33 24 33 28 12 23 1 28 12 29 3 35 33 26 15 3 19 1 23 23 32 2 27 11 7 36 34 36",
"output": "1"
},
{
"input": "100 13\n1 1 9 10 6 1 12 13 9 5 3 7 3 5 2 2 10 1 3 8 9 4 4 4 2 10 12 11 1 5 7 13 4 12 5 9 3 13 5 10 7 2 1 7 2 2 4 10 3 10 6 11 13 1 4 3 8 8 9 8 13 4 4 3 7 12 5 5 8 13 1 9 8 12 12 10 4 7 7 12 1 4 3 4 9 6 4 13 10 12 10 9 8 13 13 5 6 9 7 13",
"output": "3"
},
{
"input": "100 12\n9 12 3 3 1 3 12 12 7 9 6 5 8 12 10 7 8 3 4 8 5 9 9 10 9 7 4 5 10 7 4 1 11 6 5 9 1 2 9 9 1 10 6 8 9 10 7 9 10 3 6 4 9 12 11 10 4 4 2 12 11 8 4 9 12 6 4 7 5 1 5 2 7 4 10 2 5 6 4 2 5 8 6 9 6 4 8 6 2 11 4 12 3 1 1 11 1 6 1 10",
"output": "9"
}
] | 560 | 7,372,800 | 3 | 2,002 |
|
6 | Alice, Bob and Chocolate | [
"greedy",
"two pointers"
] | C. Alice, Bob and Chocolate | 2 | 64 | Alice and Bob like games. And now they are ready to start a new game. They have placed *n* chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob β from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.
How many bars each of the players will consume? | The first line contains one integer *n* (1<=β€<=*n*<=β€<=105) β the amount of bars on the table. The second line contains a sequence *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=1000), where *t**i* is the time (in seconds) needed to consume the *i*-th bar (in the order from left to right). | Print two numbers *a* and *b*, where *a* is the amount of bars consumed by Alice, and *b* is the amount of bars consumed by Bob. | [
"5\n2 9 8 2 7\n"
] | [
"2 3\n"
] | none | [
{
"input": "5\n2 9 8 2 7",
"output": "2 3"
},
{
"input": "1\n1",
"output": "1 0"
},
{
"input": "2\n1 1",
"output": "1 1"
},
{
"input": "3\n1 1 1",
"output": "2 1"
},
{
"input": "4\n1 1 1 1",
"output": "2 2"
},
{
"input": "1\n6",
"output": "1 0"
},
{
"input": "2\n8 2",
"output": "1 1"
},
{
"input": "3\n6 8 4",
"output": "1 2"
},
{
"input": "4\n4 3 2 8",
"output": "3 1"
},
{
"input": "5\n1 9 8 2 7",
"output": "2 3"
},
{
"input": "6\n1 4 7 3 2 1",
"output": "3 3"
},
{
"input": "11\n7 9 1 8 7 1 7 1 4 8 1",
"output": "4 7"
},
{
"input": "12\n4 2 7 1 9 5 6 2 7 8 5 7",
"output": "7 5"
},
{
"input": "13\n9 9 9 9 9 9 9 9 9 9 9 9 9",
"output": "7 6"
}
] | 62 | 0 | 0 | 2,003 |
86 | Powerful array | [
"data structures",
"implementation",
"math",
"two pointers"
] | D. Powerful array | 5 | 256 | An array of positive integers *a*1,<=*a*2,<=...,<=*a**n* is given. Let us consider its arbitrary subarray *a**l*,<=*a**l*<=+<=1...,<=*a**r*, where 1<=β€<=*l*<=β€<=*r*<=β€<=*n*. For every positive integer *s* denote by *K**s* the number of occurrences of *s* into the subarray. We call the power of the subarray the sum of products *K**s*Β·*K**s*Β·*s* for every positive integer *s*. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.
You should calculate the power of *t* given subarrays. | First line contains two integers *n* and *t* (1<=β€<=*n*,<=*t*<=β€<=200000) β the array length and the number of queries correspondingly.
Second line contains *n* positive integers *a**i* (1<=β€<=*a**i*<=β€<=106) β the elements of the array.
Next *t* lines contain two positive integers *l*, *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) each β the indices of the left and the right ends of the corresponding subarray. | Output *t* lines, the *i*-th line of the output should contain single positive integer β the power of the *i*-th query subarray.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d). | [
"3 2\n1 2 1\n1 2\n1 3\n",
"8 3\n1 1 2 2 1 3 1 1\n2 7\n1 6\n2 7\n"
] | [
"3\n6\n",
"20\n20\n20\n"
] | Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored): | [
{
"input": "3 2\n1 2 1\n1 2\n1 3",
"output": "3\n6"
},
{
"input": "8 3\n1 1 2 2 1 3 1 1\n2 7\n1 6\n2 7",
"output": "20\n20\n20"
},
{
"input": "20 8\n1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2\n4 15\n1 2\n2 20\n7 7\n13 18\n7 7\n3 19\n3 8",
"output": "108\n3\n281\n1\n27\n1\n209\n27"
},
{
"input": "10 5\n10 9 8 7 6 5 4 3 2 1\n4 8\n1 10\n3 9\n2 2\n5 10",
"output": "25\n55\n35\n9\n21"
},
{
"input": "8 10\n100 100 100 100 100 100 100 100\n1 8\n2 8\n3 8\n4 8\n5 8\n6 8\n7 8\n8 8\n1 1\n1 5",
"output": "6400\n4900\n3600\n2500\n1600\n900\n400\n100\n100\n2500"
},
{
"input": "1 2\n1\n1 1\n1 1",
"output": "1\n1"
},
{
"input": "1 1\n1000000\n1 1",
"output": "1000000"
},
{
"input": "5 15\n103 45 103 67 45\n1 1\n1 2\n1 3\n1 4\n1 5\n2 2\n2 3\n2 4\n2 5\n3 3\n3 4\n3 5\n4 4\n4 5\n5 5",
"output": "103\n148\n457\n524\n659\n45\n148\n215\n350\n103\n170\n215\n67\n112\n45"
},
{
"input": "8 10\n5 7 3 1 4 2 1 1\n5 7\n3 8\n2 7\n4 8\n4 5\n2 8\n4 6\n1 2\n4 7\n3 5",
"output": "7\n18\n20\n15\n5\n25\n7\n12\n10\n8"
},
{
"input": "9 10\n5 3 10 2 4 8 7 5 1\n6 9\n1 8\n3 3\n2 7\n3 5\n4 6\n9 9\n8 8\n1 9\n5 6",
"output": "21\n54\n10\n34\n16\n14\n1\n5\n55\n12"
},
{
"input": "9 10\n10 2 6 2 4 6 6 4 1\n4 8\n8 9\n2 3\n8 9\n9 9\n4 5\n1 7\n6 6\n6 8\n1 1",
"output": "42\n5\n8\n5\n1\n6\n76\n6\n28\n10"
},
{
"input": "10 10\n5 10 9 2 9 3 5 5 6 5\n5 10\n2 2\n3 10\n4 8\n1 3\n1 5\n5 5\n10 10\n8 9\n6 10",
"output": "63\n10\n92\n34\n24\n53\n9\n5\n11\n54"
},
{
"input": "9 10\n6 2 3 1 4 8 5 9 8\n3 9\n6 6\n9 9\n9 9\n9 9\n2 3\n3 9\n3 8\n6 9\n5 6",
"output": "54\n8\n8\n8\n8\n5\n54\n30\n46\n12"
}
] | 60 | 102,400 | -1 | 2,004 |
115 | Party | [
"dfs and similar",
"graphs",
"trees"
] | null | null | A company has *n* employees numbered from 1 to *n*. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee *A* is said to be the superior of another employee *B* if at least one of the following is true:
- Employee *A* is the immediate manager of employee *B* - Employee *B* has an immediate manager employee *C* such that employee *A* is the superior of employee *C*.
The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.
Today the company is going to arrange a party. This involves dividing all *n* employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees *A* and *B* such that *A* is the superior of *B*.
What is the minimum number of groups that must be formed? | The first line contains integer *n* (1<=β€<=*n*<=β€<=2000) β the number of employees.
The next *n* lines contain the integers *p**i* (1<=β€<=*p**i*<=β€<=*n* or *p**i*<==<=-1). Every *p**i* denotes the immediate manager for the *i*-th employee. If *p**i* is -1, that means that the *i*-th employee does not have an immediate manager.
It is guaranteed, that no employee will be the immediate manager of him/herself (*p**i*<=β <=*i*). Also, there will be no managerial cycles. | Print a single integer denoting the minimum number of groups that will be formed in the party. | [
"5\n-1\n1\n2\n1\n-1\n"
] | [
"3\n"
] | For the first example, three groups are sufficient, for example:
- Employee 1 - Employees 2 and 4 - Employees 3 and 5 | [
{
"input": "5\n-1\n1\n2\n1\n-1",
"output": "3"
},
{
"input": "4\n-1\n1\n2\n3",
"output": "4"
},
{
"input": "12\n-1\n1\n2\n3\n-1\n5\n6\n7\n-1\n9\n10\n11",
"output": "4"
},
{
"input": "6\n-1\n-1\n2\n3\n1\n1",
"output": "3"
},
{
"input": "3\n-1\n1\n1",
"output": "2"
},
{
"input": "1\n-1",
"output": "1"
},
{
"input": "2\n2\n-1",
"output": "2"
},
{
"input": "2\n-1\n-1",
"output": "1"
},
{
"input": "3\n2\n-1\n1",
"output": "3"
},
{
"input": "3\n-1\n-1\n-1",
"output": "1"
},
{
"input": "5\n4\n5\n1\n-1\n4",
"output": "3"
},
{
"input": "12\n-1\n1\n1\n1\n1\n1\n3\n4\n3\n3\n4\n7",
"output": "4"
},
{
"input": "12\n-1\n-1\n1\n-1\n1\n1\n5\n11\n8\n6\n6\n4",
"output": "5"
},
{
"input": "12\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n2\n-1\n-1\n-1",
"output": "2"
},
{
"input": "12\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1\n-1",
"output": "1"
},
{
"input": "12\n3\n4\n2\n8\n7\n1\n10\n12\n5\n-1\n9\n11",
"output": "12"
},
{
"input": "12\n5\n6\n7\n1\n-1\n9\n12\n4\n8\n-1\n3\n2",
"output": "11"
},
{
"input": "12\n-1\n9\n11\n6\n6\n-1\n6\n3\n8\n6\n1\n6",
"output": "6"
},
{
"input": "12\n7\n8\n4\n12\n7\n9\n-1\n-1\n-1\n8\n6\n-1",
"output": "3"
},
{
"input": "12\n-1\n10\n-1\n1\n-1\n5\n9\n12\n-1\n-1\n3\n-1",
"output": "2"
},
{
"input": "12\n-1\n7\n9\n12\n1\n7\n-1\n-1\n8\n5\n4\n-1",
"output": "3"
},
{
"input": "12\n11\n11\n8\n9\n1\n1\n2\n-1\n10\n3\n-1\n8",
"output": "5"
},
{
"input": "12\n-1\n8\n9\n-1\n4\n2\n11\n1\n-1\n6\n-1\n10",
"output": "6"
},
{
"input": "12\n7\n4\n4\n-1\n6\n7\n9\n7\n-1\n7\n4\n1",
"output": "4"
},
{
"input": "12\n-1\n6\n10\n6\n1\n11\n5\n9\n11\n5\n1\n9",
"output": "4"
},
{
"input": "12\n8\n6\n6\n8\n6\n-1\n6\n6\n8\n6\n8\n8",
"output": "3"
},
{
"input": "12\n3\n8\n9\n12\n2\n8\n11\n12\n2\n9\n-1\n11",
"output": "7"
},
{
"input": "7\n7\n6\n-1\n-1\n4\n4\n-1",
"output": "3"
},
{
"input": "77\n53\n-1\n48\n-1\n51\n-1\n54\n43\n70\n19\n64\n31\n45\n58\n6\n61\n41\n56\n34\n-1\n54\n-1\n66\n65\n74\n16\n38\n1\n2\n61\n-1\n34\n-1\n-1\n7\n19\n57\n13\n61\n42\n-1\n34\n65\n16\n31\n61\n6\n5\n34\n15\n-1\n13\n4\n22\n74\n7\n-1\n70\n38\n61\n45\n53\n46\n22\n-1\n65\n34\n19\n43\n48\n33\n58\n36\n-1\n47\n24\n34",
"output": "6"
},
{
"input": "5\n2\n3\n4\n5\n-1",
"output": "5"
}
] | 248 | 6,656,000 | 3 | 2,010 |
|
989 | A Tide of Riverscape | [
"constructive algorithms",
"strings"
] | null | null | "Time," Mino thinks aloud.
"What?"
"Time and tide wait for no man," explains Mino. "My name, taken from the river, always reminds me of this."
"And what are you recording?"
"You see it, tide. Everything has its own period, and I think I've figured out this one," says Mino with confidence.
Doubtfully, Kanno peeks at Mino's records.
The records are expressed as a string $s$ of characters '0', '1' and '.', where '0' denotes a low tide, '1' denotes a high tide, and '.' denotes an unknown one (either high or low).
You are to help Mino determine whether it's possible that after replacing each '.' independently with '0' or '1', a given integer $p$ is not a period of the resulting string. In case the answer is yes, please also show such a replacement to Mino.
In this problem, a positive integer $p$ is considered a period of string $s$, if for all $1 \leq i \leq \lvert s \rvert - p$, the $i$-th and $(i + p)$-th characters of $s$ are the same. Here $\lvert s \rvert$ is the length of $s$. | The first line contains two space-separated integers $n$ and $p$ ($1 \leq p \leq n \leq 2000$)Β β the length of the given string and the supposed period, respectively.
The second line contains a string $s$ of $n$ charactersΒ β Mino's records. $s$ only contains characters '0', '1' and '.', and contains at least one '.' character. | Output one lineΒ β if it's possible that $p$ is not a period of the resulting string, output any one of such strings; otherwise output "No" (without quotes, you can print letters in any case (upper or lower)). | [
"10 7\n1.0.1.0.1.\n",
"10 6\n1.0.1.1000\n",
"10 9\n1........1\n"
] | [
"1000100010\n",
"1001101000\n",
"No\n"
] | In the first example, $7$ is not a period of the resulting string because the $1$-st and $8$-th characters of it are different.
In the second example, $6$ is not a period of the resulting string because the $4$-th and $10$-th characters of it are different.
In the third example, $9$ is always a period because the only constraint that the first and last characters are the same is already satisfied.
Note that there are multiple acceptable answers for the first two examples, you can print any of them. | [
{
"input": "10 7\n1.0.1.0.1.",
"output": "1000100010"
},
{
"input": "10 6\n1.0.1.1000",
"output": "1001101000"
},
{
"input": "10 9\n1........1",
"output": "No"
},
{
"input": "1 1\n.",
"output": "No"
},
{
"input": "5 1\n0...1",
"output": "00001"
},
{
"input": "17 10\n..1.100..1..0.100",
"output": "00101000010000100"
},
{
"input": "2 1\n0.",
"output": "01"
},
{
"input": "2 1\n..",
"output": "01"
},
{
"input": "3 1\n.0.",
"output": "001"
},
{
"input": "3 1\n00.",
"output": "001"
},
{
"input": "3 2\n0..",
"output": "001"
},
{
"input": "3 2\n0.0",
"output": "No"
},
{
"input": "3 2\n1..",
"output": "100"
},
{
"input": "3 2\n.1.",
"output": "011"
},
{
"input": "3 2\n1.0",
"output": "100"
},
{
"input": "3 3\n1..",
"output": "No"
},
{
"input": "3 3\n.00",
"output": "No"
},
{
"input": "5 3\n0.000",
"output": "01000"
},
{
"input": "10 6\n10010.1001",
"output": "No"
},
{
"input": "75 38\n00.0.1.0.0110.1.00010..100.1110..110..00.0.1.0.0110.1.00010..100.1110..110.",
"output": "000001000011001000010001000111000110000000010000110010000100010001110001101"
},
{
"input": "128 108\n01100.110...000.0001.1.11.11.010010.01100.0.1.01.0.0011.11001.000101...1.0.0..100.0110.0110.0.0101.0.0.0001.01100.110...100.0001",
"output": "01100011000000000001010110110010010001100000100100000110110010000101000100000010000110001100000101000000001001100011000010000001"
},
{
"input": "5 4\n.101.",
"output": "01011"
},
{
"input": "4 2\n101.",
"output": "1011"
},
{
"input": "5 4\n.1011",
"output": "01011"
},
{
"input": "2 1\n..",
"output": "01"
},
{
"input": "5 3\n00.11",
"output": "00011"
},
{
"input": "10 8\n1111.00000",
"output": "1111000000"
},
{
"input": "10 3\n11111111.1",
"output": "1111111101"
},
{
"input": "3 2\n1.0",
"output": "100"
},
{
"input": "6 4\n11..10",
"output": "110010"
},
{
"input": "4 2\n.111",
"output": "0111"
},
{
"input": "3 2\n01.",
"output": "011"
},
{
"input": "5 4\n10.00",
"output": "10000"
},
{
"input": "10 9\n1........0",
"output": "1000000000"
},
{
"input": "2 1\n0.",
"output": "01"
},
{
"input": "8 4\n111111..",
"output": "11111100"
},
{
"input": "3 2\n0.1",
"output": "001"
},
{
"input": "4 1\n111.",
"output": "1110"
},
{
"input": "3 1\n01.",
"output": "010"
},
{
"input": "10 7\n000....111",
"output": "0000000111"
}
] | 155 | 2,969,600 | 3 | 2,013 |
Subsets and Splits