message
stringlengths
2
65.1k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
0
108k
cluster
float64
14
14
__index_level_0__
int64
0
217k
Provide tags and a correct Python 3 solution for this coding contest problem. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3
instruction
0
19,581
14
39,162
Tags: combinatorics, math Correct Solution: ``` from math import factorial res = 0 n, m, t = map(int, input().split()) def choose(n, k): return factorial(n) // (factorial(k) * factorial(n-k)) for x in range(4, n+1): if 0 < t-x <= m and x <= n: res += choose(n,x) * choose(m, t-x) print(res) ```
output
1
19,581
14
39,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` factorial = [0 for i in range(31)] factorial[0] = 1 for i in range(1,31): factorial[i] = factorial[i-1]*i def choose(n,r): numerator = factorial[n] denominator = factorial[r] * factorial[n-r] return int(numerator/denominator) n,m,t = map(int,input().split()) cnt = 0 for i in range(4, min(n,t)+1): k = t - i if((k>=1) and (k<=m)): ans = int(choose(n,i)*choose(m,k)) cnt = cnt + ans print(cnt) ```
instruction
0
19,582
14
39,164
Yes
output
1
19,582
14
39,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` n,m,t = map(int, input().split()) pas = [[1]*i for i in range(1,62)] for i in range(61): for j in range(1,i): pas[i][j]=pas[i-1][j-1]+pas[i-1][j] ans = 0 ans += pas[n+m][t] if t<=m: ans -= pas[m][t] if (t-1)<=m: ans -= n*pas[m][t-1] if (t-2)<=m: ans -= (n*(n-1))//2*(pas[m][t-2]) if (t-3)<=m: ans -= (n*(n-1)*(n-2))//6*(pas[m][t-3]) if t<=n: ans -= pas[n][t] print(ans) ```
instruction
0
19,583
14
39,166
Yes
output
1
19,583
14
39,167
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` def operation(n): if n<=1: return 1 p = 1 for i in range(1,n+1): p *= i return p def calc(n,r): return operation(n)//operation(r)//operation(n-r) stdin = input() n,m,t = stdin.split() n = int(n) m = int(m) t = int(t) ans = 0 for i in range(4,n+1): if(t-i>=1): ans += calc(n,i)*calc(m,t-i) print(ans) ```
instruction
0
19,584
14
39,168
Yes
output
1
19,584
14
39,169
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` n, m, t = map(int, input().split()) c = [[0 for j in range(31)] for i in range(31)] for i in range(31): c[i][0] = 1 c[i][i] = 1 for i in range(1, 31): for j in range(1, 31): c[i][j] = c[i-1][j-1]+c[i-1][j] # print(*c, sep = '\n') ans = 0 for i in range(4, n+1): for j in range(1, m+1): if i+j == t: ans += c[n][i]*c[m][j] print(ans) ```
instruction
0
19,585
14
39,170
Yes
output
1
19,585
14
39,171
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` from math import factorial def c(n, k): return factorial(n) // (factorial(k) * factorial(n - k)) n, m, t = map(int, input().split()) a, b = c(n, 4), c(m, 1) m -= 1 n -= 4 s = 0 if t > 5: for i in range(0, min(n, t - 5) + 1): s += c(t - 5, i) if s: print(s * a * b) else: print(a * b) ```
instruction
0
19,586
14
39,172
No
output
1
19,586
14
39,173
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` from math import factorial as f n, m, t = map(int, input().split()) ans = 0 for x in range(4, t): if x > n or n - x > m: continue ans += f(n) / f(x) / f(n - x) * f(m) / f(t - x) / f(m - t + x) print(ans) ```
instruction
0
19,587
14
39,174
No
output
1
19,587
14
39,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` n, m, t = map(int, input().split()) def count(n, m): res = 1 i = 0 while(i < m): res *= (n - i) res /= (i + 1) i += 1 return res res = 0 i = 4 while(i <= t - 1): res += count(n, i) * count(m, t - i) i += 1 print(int(res)) ```
instruction
0
19,588
14
39,176
No
output
1
19,588
14
39,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n boys and m girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly t actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different. Perform all calculations in the 64-bit type: long long for С/С++, int64 for Delphi and long for Java. Input The only line of the input data contains three integers n, m, t (4 ≤ n ≤ 30, 1 ≤ m ≤ 30, 5 ≤ t ≤ n + m). Output Find the required number of ways. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Examples Input 5 2 5 Output 10 Input 4 3 5 Output 3 Submitted Solution: ``` from math import factorial f = factorial def C(n, k): return f(n) // (f(k) * f(n - k)) n, m, t = map(int, input().split()) ans = C(n, 4) ans *= C(m, 1) n -= 4 m -= 1 for i in range(1, t + 1): #print(n, m, i, t - i) if i <= n and 0 <= t - i <= m: ans += C(n, i) * C(m, t - i) print(ans) ```
instruction
0
19,589
14
39,178
No
output
1
19,589
14
39,179
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,773
14
39,546
Tags: brute force, implementation Correct Solution: ``` arr = [] for i in range(5): arri = list(map(int, input().split())) arr.append(arri) s = [0, 1, 2, 3, 4] from itertools import permutations perm = list(permutations(s)) mx = 0 sm = 0 for i in perm: s = list(i) sm = (arr[s[0]][s[1]]+arr[s[1]][s[0]]+arr[s[2]][s[3]]+arr[s[3]][s[2]])+(arr[s[1]][s[2]]+arr[s[2]][s[1]]+arr[s[3]][s[4]]+arr[s[4]][s[3]])+(arr[s[2]][s[3]]+arr[s[3]][s[2]])+(arr[s[3]][s[4]]+arr[s[4]][s[3]]) if mx < sm: mx = sm print(mx) ```
output
1
19,773
14
39,547
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,774
14
39,548
Tags: brute force, implementation Correct Solution: ``` from itertools import permutations g = [list(map(int, input().split())) for i in range(5)] j = lambda p: g[p[0]][p[1]] + g[p[1]][p[0]] + 2 * (g[p[2]][p[3]] + g[p[3]][p[2]]) + g[p[1]][p[2]] + g[p[2]][p[1]] + 2 * (g[p[3]][p[4]] + g[p[4]][p[3]]) print(max(j(p) for p in permutations(range(5)))) ```
output
1
19,774
14
39,549
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,775
14
39,550
Tags: brute force, implementation Correct Solution: ``` import itertools g = [[[] for _ in range(5)] for _ in range(5)] for i in range(5): gi = [int(gij) for gij in input().split(" ")] for j in range(5): gij = gi[j] g[i][j] = gij p = itertools.permutations([1,2,3,4,5]) ans = -1 for x in p: pi = list(x) pi = [pi-1 for pi in pi] tmp = 0 tmp += g[pi[0]][pi[1]] tmp += g[pi[1]][pi[0]] tmp += g[pi[1]][pi[2]] tmp += g[pi[2]][pi[1]] tmp += g[pi[2]][pi[3]] tmp += g[pi[3]][pi[2]] tmp += g[pi[2]][pi[3]] tmp += g[pi[3]][pi[2]] tmp += g[pi[3]][pi[4]] tmp += g[pi[4]][pi[3]] tmp += g[pi[3]][pi[4]] tmp += g[pi[4]][pi[3]] if tmp > ans: ans = tmp print(ans) ```
output
1
19,775
14
39,551
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,776
14
39,552
Tags: brute force, implementation Correct Solution: ``` import itertools g = [] for i in range(5): g.append(list(map(int, input().split(' ')))) def c(order): happy = 0 for i in range(5): for j in range(0, len(order), 2): if j + 1 < len(order): happy += g[order[j]][order[j + 1]] + g[order[j+1]][order[j]] order = order[1:] return happy mx = 0 for x in itertools.permutations([i for i in range(5)]): mx = max(mx, c(x)) print(mx) ```
output
1
19,776
14
39,553
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,777
14
39,554
Tags: brute force, implementation Correct Solution: ``` from itertools import permutations def main(grid): return max(get_total_happiness(line, grid) for line in generate_lines()) def get_total_happiness(line, grid): total = 0 while line: for a, b in zip(line[::2], line[1::2]): total += grid[a][b] + grid[b][a] del line[0] return total def generate_lines(): for line in permutations(range(5)): yield list(line) if __name__ == "__main__": grid = [list(map(int, input().split())) for _ in range(5)] print(main(grid)) ```
output
1
19,777
14
39,555
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,778
14
39,556
Tags: brute force, implementation Correct Solution: ``` from itertools import permutations arr = [] for _ in range(5): arr.append(list(map(int, input().split()))) permutation = list(permutations([1, 2, 3, 4, 5])) maxsum = 0 sum = 0 for i in range(len(permutation)): first = permutation[i][0]-1 second = permutation[i][1]-1 third = permutation[i][2]-1 four = permutation[i][3]-1 five = permutation[i][4]-1 sum += arr[first][second] sum += arr[second][first] sum += arr[third][four]*2 sum += arr[four][third]*2 sum += arr[second][third] sum += arr[third][second] sum += arr[four][five]*2 sum += arr[five][four]*2 if sum > maxsum: maxsum = sum sum = 0 print(maxsum) ```
output
1
19,778
14
39,557
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,779
14
39,558
Tags: brute force, implementation Correct Solution: ``` # coding: utf-8 import itertools g = [] for i in range(5): g.append([int(i) for i in input().split()]) permutations = list(itertools.permutations([0,1,2,3,4],5)) ans = -1 for per in permutations: tmp = (g[per[0]][per[1]]+g[per[1]][per[0]])*1\ + (g[per[1]][per[2]]+g[per[2]][per[1]])*1\ + (g[per[2]][per[3]]+g[per[3]][per[2]])*2\ + (g[per[3]][per[4]]+g[per[4]][per[3]])*2 if tmp > ans: ans = tmp print(ans) ```
output
1
19,779
14
39,559
Provide tags and a correct Python 3 solution for this coding contest problem. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
instruction
0
19,780
14
39,560
Tags: brute force, implementation Correct Solution: ``` """ Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away. """ import sys input = sys.stdin.readline # from bisect import bisect_left as lower_bound; # from bisect import bisect_right as upper_bound; # from math import ceil, factorial; def ceil(x): if x != int(x): x = int(x) + 1; return x; def factorial(x, m): val = 1 while x>0: val = (val * x) % m x -= 1 return val # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; ## gcd function def gcd(a,b): if b == 0: return a; return gcd(b, a % b); ## nCr function efficient using Binomial Cofficient def nCr(n, k): if(k > n - k): k = n - k; res = 1; for i in range(k): res = res * (n - i); res = res / (i + 1); return int(res); ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0, hi = None): if hi == None: hi = len(a); while lo < hi: mid = (lo+hi)//2; if a[mid] < x: lo = mid+1; else: hi = mid; return lo; ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0 and n > 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0 and n > 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b; # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e5 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve(); def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret; ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())); def float_array(): return list(map(float, input().strip().split())); ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### from itertools import permutations def solve(): a = [list(map(int, input().split())) for i in range(5)] lis = list(permutations([1, 2, 3, 4, 5], 5)) # print(list(lis)) ans = 0 for x in lis: # print(x[0], x[1], x[2], x[3], x[4]) val = a[x[0] - 1][x[1] - 1] + a[x[1] - 1][x[0] - 1] + a[x[2] - 1][x[3] - 1] + a[x[3] - 1][x[2] - 1] + a[x[1] - 1][x[2] - 1] + a[x[2] - 1][x[1] - 1] + a[x[3] - 1][x[4] - 1] + a[x[4] - 1][x[3] - 1] + a[x[2] - 1][x[3] - 1] + a[x[3] - 1][x[2] - 1] + a[x[3] - 1][x[4] - 1] + a[x[4] - 1][x[3] - 1] ans = max(val, ans) print(ans) if __name__ == '__main__': for _ in range(1): solve() # fin_time = datetime.now() # print("Execution time (for loop): ", (fin_time-init_time)) ```
output
1
19,780
14
39,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` from itertools import permutations ans = -1 happies = [list(map(int, input().split(" "))) for i in range(5)] perm = list(permutations([0,1,2,3,4])) for a,b,c,d,e in perm: cur = happies[a][b]+happies[b][a] cur += happies[b][c]+happies[c][b] cur += (happies[c][d]+happies[d][c]) *2 cur += (happies[e][d]+happies[d][e]) *2 if cur > ans: ans = cur print(ans) ```
instruction
0
19,781
14
39,562
Yes
output
1
19,781
14
39,563
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` g = [list(map(int,input().split())) for _ in range(5)] from itertools import permutations as P mx = 0 for per in P([0,1,2,3,4]): a,b,c,d,e = per mx = max(mx,g[a][b]+g[b][a]+g[b][c]+g[c][b]+\ 2*(g[e][d]+g[d][e]+g[d][c]+g[c][d])) print(mx) ```
instruction
0
19,782
14
39,564
Yes
output
1
19,782
14
39,565
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` def arr_2d(n): return [[int(x) for x in stdin.readline().split()] for i in range(n)] from sys import * from itertools import * arr, perm, ans = arr_2d(5), list(permutations([x for x in range(5)], 5)), 0 for i in perm: eq = arr[i[0]][i[1]] + arr[i[1]][i[0]] + arr[i[1]][i[2]] + arr[i[2]][i[1]] + ( (arr[i[3]][i[2]] + arr[i[2]][i[3]]) * 2) + ((arr[i[4]][i[3]] + arr[i[3]][i[4]]) * 2) ans=max(ans,eq) print(ans) ```
instruction
0
19,783
14
39,566
Yes
output
1
19,783
14
39,567
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` x=0 from itertools import permutations z=[ [int(j) for j in input().split()] for i in range(5)] m=0 for k in permutations([0,1,2,3,4]): s=0 for i in range(4): for j in range(i , 4 , 2): s+=z [ k[j] ] [k[j+1]] + z [ k[j+1] ] [k[j]] if s > m: m=s print(m) ```
instruction
0
19,784
14
39,568
Yes
output
1
19,784
14
39,569
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` from itertools import permutations a = [] for _ in range(5): a.append(list(map(int,input().split()))) #print(a) #print(list(zip(*a))) s = 0 for i in permutations('01234',5): b = [] print(i) for j in i: b.append(a[int(j)]) # print(b) b = list(zip(*b)) # print(b) c = [] for j in i: c.append(b[int(j)]) #print(c[0][1]) # print(c) s = max(s,c[0][1]+c[1][0]+c[1][2]+c[2][1]+2*(c[2][3]+c[3][2]+c[3][4]+c[4][3])) print(s) ```
instruction
0
19,785
14
39,570
No
output
1
19,785
14
39,571
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` from sys import * from itertools import permutations input = lambda:stdin.readline() int_arr = lambda : list(map(int,stdin.readline().strip().split())) str_arr = lambda :list(map(str,stdin.readline().split())) get_str = lambda : map(str,stdin.readline().strip().split()) get_int = lambda: map(int,stdin.readline().strip().split()) get_float = lambda : map(float,stdin.readline().strip().split()) mod = 1000000007 setrecursionlimit(1000) def next_permutation(L): n = len(L) i = n - 2 while i >= 0 and L[i] >= L[i+1]: i -= 1 if i == -1: return False j = i + 1 while j < n and L[j] > L[i]: j += 1 j -= 1 L[i], L[j] = L[j], L[i] left = i + 1 right = n - 1 while left < right: L[left], L[right] = L[right], L[left] left += 1 right -= 1 return True n = 5 mat = [] for i in range(n): mat.append(int_arr()) arr = [0,1,2,3,4] mx = float('-inf') while next_permutation(arr): val = mat[arr[0]][arr[1]] + mat[arr[1]][arr[0]] + mat[arr[2]][arr[3]] + mat[arr[3]][arr[2]] + mat[arr[1]][arr[2]] + mat[arr[2]][arr[1]] + mat[arr[3]][arr[4]] + mat[arr[4]][arr[3]] + mat[arr[2]][arr[3]] + mat[arr[3]][arr[2]] + mat[arr[3]][arr[4]] + mat[arr[4]][arr[3]] mx = max(mx,val) print(mx) ```
instruction
0
19,786
14
39,572
No
output
1
19,786
14
39,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` def next_permutation(a): k = len(a) - 2 while a[k] > a[k+1]: k -= 1 t = k + 1 while t < len(a) - 1 and a[t+1] > a[k]: t += 1 a[t], a[k] = a[k], a[t] sub = a[k+1:] sub.reverse() return a[:k+1] + sub def count_current(a, x): r = 0 for i in range(len(a) // 2): f = a[2*i] n = a[2*i + 1] r += x[f][n] + x[n][f] return r def count(a, x): r, t = 0, a while len(t) > 1: r += count_current(t, x) t = t[1:] return r arr = [] for i in range(5): arr.append([int(c) for c in input().split()]) p = [0, 1, 2, 3, 4] res = 0 while p != [4, 3, 2, 1, 0]: res = max(res, count(p, arr)) p = next_permutation(p) print(res) ```
instruction
0
19,787
14
39,574
No
output
1
19,787
14
39,575
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower. Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower. We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower. Input The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i. Assume that the students are numbered from 1 to 5. Output Print a single integer — the maximum possible total happiness of the students. Examples Input 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 Output 32 Input 0 43 21 18 2 3 0 21 11 65 5 2 0 1 4 54 62 12 0 99 87 64 81 33 0 Output 620 Note In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals: (g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32. Submitted Solution: ``` import sys import itertools data = sys.stdin.readlines() g = [] for i in data: g += [[int(x) for x in i.rstrip('\n').split(' ')]] a = 0 perms = itertools.permutations('01234') for perm in perms: perm = [int(x) for x in perm] # (01)(23)4 tmp = g[perm[0]][perm[1]] + g[perm[1]][perm[0]] tmp += g[perm[2]][perm[3]] + g[perm[3]][perm[2]] # [0](12)(34) tmp += g[perm[1]][perm[2]] + g[perm[2]][perm[1]] tmp += g[perm[3]][perm[4]] + g[perm[4]][perm[3]] # 0[1](23)4 tmp += g[perm[2]][perm[3]] + g[perm[3]][perm[2]] # 01[2](34) tmp += g[perm[3]][perm[4]] + g[perm[4]][perm[3]] print ("p " + str(perm) + " a " + str(tmp)) if tmp > a: a = tmp print(a) ```
instruction
0
19,788
14
39,576
No
output
1
19,788
14
39,577
Provide tags and a correct Python 3 solution for this coding contest problem. Vasya bought the collected works of a well-known Berland poet Petya in n volumes. The volumes are numbered from 1 to n. He thinks that it does not do to arrange the book simply according to their order. Vasya wants to minimize the number of the disposition’s divisors — the positive integers i such that for at least one j (1 ≤ j ≤ n) is true both: j mod i = 0 and at the same time p(j) mod i = 0, where p(j) is the number of the tome that stands on the j-th place and mod is the operation of taking the division remainder. Naturally, one volume can occupy exactly one place and in one place can stand exactly one volume. Help Vasya — find the volume disposition with the minimum number of divisors. Input The first line contains number n (1 ≤ n ≤ 100000) which represents the number of volumes and free places. Output Print n numbers — the sought disposition with the minimum divisor number. The j-th number (1 ≤ j ≤ n) should be equal to p(j) — the number of tome that stands on the j-th place. If there are several solutions, print any of them. Examples Input 2 Output 2 1 Input 3 Output 1 3 2
instruction
0
19,810
14
39,620
Tags: constructive algorithms, math Correct Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction from collections import defaultdict BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #-------------------game starts now----------------------------------------------------- n=int(input()) l=[i for i in range(1,n)] l=[n]+l print(*l,sep=" ") ```
output
1
19,810
14
39,621
Provide tags and a correct Python 3 solution for this coding contest problem. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake.
instruction
0
19,843
14
39,686
Tags: implementation Correct Solution: ``` R = lambda : map(int, input().split()) n,m = R() f,b = R(),list(R()) from collections import defaultdict def solve(f,b): df = defaultdict(list) for i,fi in enumerate(f,1): df[fi].append(i) r = [] for bi in b: if bi not in df: print("Impossible") return for bi in b: if len(df[bi]) > 1: print("Ambiguity") return for bi in b: r.append(df[bi][0]) print("Possible") print(" ".join(map(str,r))) solve(f,b) ```
output
1
19,843
14
39,687
Provide tags and a correct Python 3 solution for this coding contest problem. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake.
instruction
0
19,844
14
39,688
Tags: implementation Correct Solution: ``` def main(): n, m = map(int, input().split()) xlat, res, s = [-1] * (n + 1), [0] * m, "Possible" for i, f in enumerate(map(int, input().split()), 1): x = xlat[f] if x == -1: xlat[f] = i elif x > -1: xlat[f] = -2 for i, b in enumerate(map(int, input().split())): x = xlat[b] if x >= 0: res[i] = x else: if x == -1: print("Impossible") return s = "Ambiguity" print(s) if s is "Possible": print(' '.join(map(str, res))) if __name__ == '__main__': main() ```
output
1
19,844
14
39,689
Provide tags and a correct Python 3 solution for this coding contest problem. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake.
instruction
0
19,846
14
39,692
Tags: implementation Correct Solution: ``` # Author : nitish420 -------------------------------------------------------------------- import os import sys from io import BytesIO, IOBase # mod=10**9+7 # sys.setrecursionlimit(10**6) # mxm=sys.maxsize # from functools import lru_cache def main(): n,m=map(int,input().split()) f=list(map(int,input().split())) b=list(map(int,input().split())) a=[-1]*(m) c=[-1]*(n+1) for i,item in enumerate(f): if c[item]==-1: c[item]=[] c[item].append([i+1]) flag=1 for i in range(m): if c[b[i]]==-1: flag=0 break elif len(c[b[i]])>1: flag=2 else: a[i]=c[b[i]][0][0] if flag: if flag==1: print("Possible") print(*a) else: print("Ambiguity") else: print("Impossible") # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = 'x' in file.mode or 'r' not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b'\n') + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda: self.buffer.read().decode('ascii') self.readline = lambda: self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # endregion if __name__ == '__main__': main() ```
output
1
19,846
14
39,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake. Submitted Solution: ``` def BuildHashTable(n,f): # Initializing HashTable - htab htab = [] for i in range(n): htab[len(htab):] = [[0,-1]] # Parsing sequence f to update htab for i in range(n): j = f[i] # setting the value of current element of sequence f # Updating occurrence of current value in hash table htab[j-1][0] = htab[j-1][0] + 1 # Updating the position of current value in hash table # if value isn't found in f, position is -1. # else if value is found only once in f, set the position to its index # else (value is found multiple times in f), set the position to -2 if htab[j-1][1] == -1: htab[j-1][1] = i + 1 else: htab[j-1][1] = -2 return htab def LookupAndTell(htab, b): # Function to look up the sequence B and tell us if it is present in F or not using HashTable # status = 0 # status decides if a termination condition has been reached halfway -- This is wrong condition to check. Must go till the end of sequence state = 1 i = 0 a = [0]*len(b) # Initialize output sequence A while (i < len(b) and state != 0): j = b[i] # Setting the lookup value from sequence B if (htab[j-1][0] == 0): # Case when current element of B is not present in F print ('Impossible') state = 0 elif (htab[j-1][1] < -1): # Case when current element of B is present in F but occurs in multiple positions leading to ambiguity state = 2 else: # Case when current element of B is uniquely present in F a[i] = htab[j-1][1] i = i + 1 # Print output for the case when it is possible to recover a unique sequence A if (state == 1 ): print ('Possible') print (' '.join(map(str, a))) elif (state == 2): print ('Ambiguity') return def CheckSeq(n, m, f, b): status = 1 if (len(f) != n or len(b) != m): print ('Impossible') status = 0 if (status == 1): for i in range(n): if (f[i] > n or f[i] < 1): print ('Impossible') status = 0 for j in range(m): if (b[j] > n or b[j] < 1): print ('Impossible') status = 0 return status def main(): # Receiving inputs n,m = map(int, input().split( )) f = list(map(int, input().split( ))) b = list(map(int, input().split( ))) # Check if sequences F and B have entries in the range 1 to n check = CheckSeq(n, m, f, b) # Solving the problem if (check == 1): h = BuildHashTable(n,f) LookupAndTell(h, b) if __name__ == '__main__': main() ```
instruction
0
19,849
14
39,698
Yes
output
1
19,849
14
39,699
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake. Submitted Solution: ``` def BuildHashTable(n,f): # Initializing HashTable - htab htab = [] for i in range(n): htab[len(htab):] = [[0,-1]] # Parsing sequence f to update htab for i in range(n): j = f[i] # setting the value of current element of sequence f # Updating occurrence of current value in hash table htab[j-1][0] = htab[j-1][0] + 1 # Updating the position of current value in hash table # if value isn't found in f, position is -1. # else if value is found only once in f, set the position to its index # else (value is found multiple times in f), set the position to -2 if htab[j-1][1] == -1: htab[j-1][1] = i + 1 else: htab[j-1][1] = -2 return htab def LookupAndTell(htab, b): # Function to look up the sequence B and tell us if it is present in F or not using HashTable # status = 0 # status decides if a termination condition has been reached halfway -- This is wrong condition to check. Must go till the end of sequence state = 1 i = 0 a = [0]*len(b) # Initialize output sequence A while (i < len(b) and state != 0): j = b[i] # Setting the lookup value from sequence B if (htab[j-1][0] == 0): # Case when current element of B is not present in F print ('Impossible') state = 0 elif (htab[j-1][1] < -1): # Case when current element of B is present in F but occurs in multiple positions leading to ambiguity state = 2 else: # Case when current element of B is uniquely present in F a[i] = htab[j-1][1] i = i + 1 # Print output for the cases when sequence A exists if (state == 1 ): print ('Possible') print (' '.join(map(str, a))) elif (state == 2): print ('Ambiguity') return def main(): # Receiving inputs n,m = map(int, input().split( )) f = list(map(int, input().split( ))) b = list(map(int, input().split( ))) # Solving the problem h = BuildHashTable(n,f) LookupAndTell(h, b) if __name__ == '__main__': main() ```
instruction
0
19,850
14
39,700
Yes
output
1
19,850
14
39,701
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake. Submitted Solution: ``` def BuildHashTable(n,f): # Initializing HashTable - htab htab = [] for i in range(n): htab[len(htab):] = [[0,-1]] # Parsing sequence f to update htab for i in range(n): j = int(f[i]) # setting the value of current element of sequence f # Updating occurrence of current value in hash table htab[j-1][0] = htab[j-1][0] + 1 # Updating the position of current value in hash table # if value isn't found in f, position is -1. # else if value is found only once in f, set the position to its index # else (value is found multiple times in f), set the position to -2 if htab[j-1][1] == -1: htab[j-1][1] = i + 1 else: htab[j-1][1] = -2 return htab def LookupAndTell(htab, b): # Function to look up the sequence B and tell us if it is present in F or not using HashTable status = 0 # status decides if a termination condition has been reached halfway i = 0 a = [] # Initialize output sequence A while (i < len(b) and status == 0): j = int(b[i]) # Setting the lookup value from sequence B if (htab[j-1][0] == 0): # Case when current element of B is not present in F print ('Impossible') status = 1 elif (htab[j-1][1] < -1): # Case when current element of B is present in F but occurs in multiple positions leading to ambiguity print ('Ambiguity') status = 1 else: # Case when current element of B is uniquely present in F a = a + [htab[j-1][1]] i = i + 1 # Print output for the case when it is possible to recover a unique sequence A if (status == 0 ): print ('Possible') print (a) return def main(): n,m = map(int, input().split( )) f = list(map(int, input().split( ))) b = list(map(int, input().split( ))) h = BuildHashTable(n,f) LookupAndTell(h, b) if __name__ == '__main__': main() ```
instruction
0
19,851
14
39,702
No
output
1
19,851
14
39,703
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1, a2, ..., am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1, f2, ..., fn of length n and for each number ai got number bi = fai. To finish the prank he erased the initial sequence ai. It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible. Input The first line of the input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the lengths of sequences fi and bi respectively. The second line contains n integers, determining sequence f1, f2, ..., fn (1 ≤ fi ≤ n). The last line contains m integers, determining sequence b1, b2, ..., bm (1 ≤ bi ≤ n). Output Print "Possible" if there is exactly one sequence ai, such that bi = fai for all i from 1 to m. Then print m integers a1, a2, ..., am. If there are multiple suitable sequences ai, print "Ambiguity". If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible". Examples Input 3 3 3 2 1 1 2 3 Output Possible 3 2 1 Input 3 3 1 1 1 1 1 1 Output Ambiguity Input 3 3 1 2 1 3 3 3 Output Impossible Note In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique. In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence. In the third sample fi ≠ 3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake. Submitted Solution: ``` l=list(map(int,input().split(' '))) A=list(map(int,input().split(' '))) B=list(map(int,input().split(' '))) n=l[0] m=l[1] key=0 flag=0 for i in range(len(A)): if A[0]!=A[i]: key=1 if key==1: for j in range(len(A)): if A.count(A[j])!=1: flag=1 if flag==1: print('Impossible') else: print('Possible') else: print('Ambiguity') ```
instruction
0
19,853
14
39,706
No
output
1
19,853
14
39,707
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,906
14
39,812
Tags: expression parsing, implementation, strings Correct Solution: ``` def main(): my_name = input() actions = (input().split() for _ in range(int(input()))) priority_factor = {} rewards = { "posted" : 15, "commented" : 10, "likes" : 5 } all_names = {my_name} for act in actions: X = act[0] Y = act[2 if act[1] == 'likes' else 3] Y = Y[:-2] all_names.add(X) all_names.add(Y) if Y == my_name: X, Y = Y, X if X == my_name: priority_factor.setdefault(Y, 0) priority_factor[Y] += rewards[act[1]] all_names.remove(my_name) def key(y): return (-priority_factor[y] if y in priority_factor else 0, y) print(*sorted(all_names, key=key), sep="\n") if __name__ == "__main__": main() ```
output
1
19,906
14
39,813
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,907
14
39,814
Tags: expression parsing, implementation, strings Correct Solution: ``` myname=input();n=int(input());priority={} def func(action): if action[1]=="posted":k=15 else: k=10 if action[1]=="posted" or action[1]=="commented": name2,s=[x for x in action[3].split("'")] if action[0]==myname or name2==myname: if action[0] in priority: priority[action[0]]+=k else: priority[action[0]]=k if name2 in priority: priority[name2]+=k else: priority[name2]=k else: if action[0] not in priority: priority[action[0]]=0 if name2 not in priority: priority[name2]=0 else: name2,s=[x for x in action[2].split("'")] if action[0]==myname or name2==myname: if action[0] in priority: priority[action[0]]+=5 else: priority[action[0]]=5 if name2 in priority: priority[name2]+=5 else: priority[name2]=5 else: if action[0] not in priority: priority[action[0]]=0 if name2 not in priority: priority[name2]=0 while n>0: n-=1 action=[x for x in input().split(" ")] func(action) try: priority.pop(myname) except KeyError as e: pass k=[];v=[] for key,val in priority.items(): k.append(key) v.append(val) n=len(v) for i in range(n-1): swapped=False for j in range(n-1-i): if v[j]<v[j+1]: v[j],v[j+1]=v[j+1],v[j] k[j],k[j+1]=k[j+1],k[j] swapped=True elif v[j]==v[j+1]: if k[j]>k[j+1]: v[j],v[j+1]=v[j+1],v[j] k[j],k[j+1]=k[j+1],k[j] swapped=True if not swapped: break for item in k: print(item) ```
output
1
19,907
14
39,815
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,908
14
39,816
Tags: expression parsing, implementation, strings Correct Solution: ``` acttopoint={'posted':15,'commented':10,'likes':5} def addrait(rait,item,point): if item in rait: rait[item]+=point else: rait[item]=point def getnames(s): end=s.find(' ') fname=s[:end] beg=s.find(' ',end+1) point=acttopoint[s[end+1:beg]] if point>5: beg=s.find(' ',beg+1) end=s.find('\'') sname=s[beg+1:end] return fname,sname,int(point) myname=input() n=int(input()) v=[] for c in range(n): v.append(getnames(input())) rait=dict() for c in v: tar=-1 if c[0]==myname: tar=1 elif c[1]==myname: tar=0 if tar==-1: addrait(rait,c[0],0) addrait(rait,c[1],0) continue addrait(rait,c[tar],c[2]) v=list(rait.items()) v.sort(key=lambda x: (-x[1],x[0])) for c in v: print(c[0]) ```
output
1
19,908
14
39,817
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,909
14
39,818
Tags: expression parsing, implementation, strings Correct Solution: ``` me = input() n = int(input()) names = {} point = {'posted':15, 'commented':10, 'likes':5} for _ in range(n): data = input().split() if data[1]=='likes': b = data[2][:-2] else: b = data[3][:-2] if data[0]==me and b!=me: if b in names: names[b] += point[data[1]] else: names[b] = point[data[1]] elif data[0]!=me and b==me: if data[0] in names: names[data[0]] += point[data[1]] else: names[data[0]] = point[data[1]] elif data[0]!=me and b!=me: if data[0] not in names: names[data[0]] = 0 if b not in names: names[b] = 0 prior = [] for i in names: prior.append((i, names[i])) prior = sorted(prior, key=lambda x: (-x[1], x[0])) for i in prior: print(i[0]) ```
output
1
19,909
14
39,819
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,910
14
39,820
Tags: expression parsing, implementation, strings Correct Solution: ``` ans=[] me=input() pri={} d={'p':15,'c':10,'l':5} n=int(input()) for i in range(n): s=input().split() a=s[0] b=s[1] c=s[-2][:-2] if a==me or c==me: if a==me: if c in pri: pri[c]-=d[b[0]] else: pri[c]=-d[b[0]] else: if a in pri: pri[a]-=d[b[0]] else: pri[a]=-d[b[0]] else: if a not in pri: pri[a]=0 if c not in pri: pri[c]=0 #print(pri) for (v,k) in sorted((v,k) for (k,v) in pri.items()): print(k) ```
output
1
19,910
14
39,821
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,911
14
39,822
Tags: expression parsing, implementation, strings Correct Solution: ``` import sys def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def tinput(): return input().split() def rinput(): return map(int, tinput()) def rlinput(): return list(rinput()) from collections import defaultdict name = input() n = iinput() dict = defaultdict(int) def changes(dict,name1,name2,val): if name1 == name: dict[name2] += val elif name2 == name: dict[name1] += val else: dict[name1] += 0 dict[name2] += 0 def get_names(A): return A[0],A[-2][:-2] for _ in range(n): A = list(input().split()) name1,name2 = get_names(A) if A[1] == 'posted': changes(dict,name1,name2,15) if A[1] == 'commented': changes(dict,name1,name2,10) if A[1] == 'likes': changes(dict,name1,name2,5) lists = list(dict.items()) lists.sort(key = lambda x : (-x[1],x[0])) for l in lists: print(l[0]) ```
output
1
19,911
14
39,823
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,912
14
39,824
Tags: expression parsing, implementation, strings Correct Solution: ``` n=input() t=int(input()) l=[] for i in range(t): l.append(input()) result={n:0} for j in l: act = j.split() if act[0] not in result: result[act[0]]=0 if act[-2][:-2] not in result: result[act[-2][:-2]]=0 if act[0]==n or act[-2][:-2]==n: score=0 if act[1]=="posted": score+=15 elif act[1]=="commented": score+=10 else: score+=5 result[act[0]]+=score result[act[-2][:-2]]+=score score=0 result.pop(n) for i in sorted(result.items(),key=lambda x:(-x[1],x[0])): print(i[0]) ```
output
1
19,912
14
39,825
Provide tags and a correct Python 3 solution for this coding contest problem. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted
instruction
0
19,913
14
39,826
Tags: expression parsing, implementation, strings Correct Solution: ``` #from dust i have come dust i will be class pair: def __init__(self,f,s): self.f=f self.s=s me=input() n=int(input()) mp={} for i in range(n): a=list(map(str,input().split())) if a[1]=="likes": ot=a[2] else: ot=a[3] #parse the name her="" for j in range(len(ot)-2): her+=ot[j] if me!=a[0] and me!=her: try: x=mp.get(a[0]) mp[a[0]]+=0 except: mp[a[0]]=0 try: x=mp.get(her) mp[her]+=0 except: mp[her]=0 continue if a[1]=="posted": try: x=mp.get(a[0]) mp[a[0]]+=15 except: mp[a[0]]=15 try: x=mp.get(a[0]) mp[her]+=15 except: mp[her]=15 elif a[1]=="commented": try: x = mp.get(a[0]) mp[a[0]] += 10 except: mp[a[0]] = 10 try: x = mp.get(a[0]) mp[her] += 10 except: mp[her] = 10 else: try: x = mp.get(a[0]) mp[a[0]] += 5 except: mp[a[0]] = 5 try: x = mp.get(a[0]) mp[her] += 5 except: mp[her] = 5 names=[] for i in mp: if i != me: names.append(pair(mp[i],i)) l=sorted(names,key=lambda x:(-x.f,x.s)) for i in range(len(names)): print(l[i].s) ```
output
1
19,913
14
39,827
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` name=input() n=int(input()) d=dict() for _ in range(n): s=input() t=s.split() a,b=t[0],t[-2][:-2] typ=15 if s.count("posted on") else 10 if s.count("commented on") else 5 if(b not in d.keys()):d[b]=0 if(a not in d.keys()):d[a]=0 if(a==name): d[b]+=typ elif(b==name): d[a]+=typ vl=[] for e in d.keys(): vl.append([-d[e],e]) vl.sort() for e in vl: if(e[1]==name):continue print(e[1]) ```
instruction
0
19,914
14
39,828
Yes
output
1
19,914
14
39,829
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` from collections import * from operator import * myname, n, mem = input(), int(input()), defaultdict(int) for i in range(n): s, val = input().split(), 0 x, y = s[0], s[3][:-2] if s[1] == 'posted': val = 15 elif s[1] == 'commented': val = 10 else: val = 5 y = s[2][:-2] if x == myname: mem[y] += val elif y == myname: mem[x] += val else: if not mem[x]: mem[x] = 0 if not mem[y]: mem[y] = 0 ans = sorted(list(mem.items()), reverse=True, key=itemgetter(1, 0)) mem = defaultdict(list) for i, j in ans: mem[j].append(i) for i, j in mem.items(): for k in j[::-1]: print(k) ```
instruction
0
19,915
14
39,830
Yes
output
1
19,915
14
39,831
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` s = input() n = int(input()) l = [] for i in range(n): k = input().split() if len(k) == 5: a = k[0] b = k[3][:-2] f = True f2 = True for q in range(len(l)): if a == l[q][0]: f = False if b == l[q][0]: f2 = False if f and a != s: l.append([a,0]) if f2 and b != s: l.append([b,0]) score = 10 if k[1] == 'posted': score = 15 for u in range(len(l)): if l[u][0] == a and b == s: l[u][1]+=score if l[u][0] == b and a == s: l[u][1]+=score if len(k) == 4: a = k[0] b = k[2][:-2] f = True f2 = True for q in range(len(l)): if a == l[q][0]: f = False if b == l[q][0]: f2 = False if f and a != s: l.append([a,0]) if f2 and b != s: l.append([b,0]) score = 5 for u in range(len(l)): if l[u][0] == a and b == s: l[u][1]+=score if l[u][0] == b and a == s: l[u][1]+=score i = 0 while i < len(l): j = 0 while j < len(l)-1: if l[j][1]<l[j+1][1]: k = l[j] l[j] = l[j+1] l[j+1] = k if l[j][1] == l[j+1][1]: if l[j][0] > l[j+1][0]: k = l[j] l[j] = l[j+1] l[j+1] =k j+=1 i+=1 for i in l: print(i[0]) ```
instruction
0
19,916
14
39,832
Yes
output
1
19,916
14
39,833
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` from collections import Counter name = input() n = int(input()) cnt = Counter() points = {'posted': 15, 'commented': 10, 'likes': 5} for _ in range(n): action = input().split() subj = action[0] dest = action[3] if len(action) == 5 else action[2] dest = dest[:-2] p = points[action[1]] if subj == name: cnt[dest] += p elif dest == name: cnt[subj] += p else: cnt[subj] += 0 cnt[dest] += 0 ans = sorted(((v, k) for k, v in cnt.items()), key = lambda x: (-x[0], x[1])) print('\n'.join(x[1] for x in ans)) ```
instruction
0
19,917
14
39,834
Yes
output
1
19,917
14
39,835
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` s=input() n=int(input()) dict1={} for i in range(n): ss=input() ll=ss.split() if(len(ll)==4): a1=ll[0] a2=ll[2][0:len(ll[2])-2] if(a1==s): if(a2 in dict1): dict1[a2]+=5 else: dict1[a2]=5 elif(a2==s): if(a1 in dict1): dict1[a1]+=5 else: dict1[a1]=5 else: if(a1 in dict1): dict1[a1]+=0 else: dict1[a1]=0 if(a2 in dict1): dict1[a2]+=0 else: dict1[a2]=0 else: a1=ll[0] a2=ll[3][0:len(ll[3])-2] if(ll[-1]=="wall"): c=15 else: c=10 if(a1==s): if(a2 in dict1): dict1[a2]+=c else: dict1[a2]=c elif(a2==s): if(a1 in dict1): dict1[a1]+=c else: dict1[a1]=c else: if(a1 in dict1): dict1[a1]+=0 else: dict1[a1]=0 if(a2 in dict1): dict1[a2]+=0 else: dict1[a2]=0 flist=[] for i in dict1: flist.append((dict1[i],i)) flist=sorted(flist,key=lambda s:s[0],reverse=True) flist=sorted(flist,key=lambda s:s[1]) for i in range(len(flist)): print(flist[i][1]) ```
instruction
0
19,918
14
39,836
No
output
1
19,918
14
39,837
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` import bisect import os from collections import Counter import bisect from collections import defaultdict import math import random import heapq as hq from math import sqrt import sys from functools import reduce, cmp_to_key from collections import deque import threading from itertools import combinations from io import BytesIO, IOBase from itertools import accumulate # sys.setrecursionlimit(200000) # mod = 10**9+7 # mod = 987 def primeFactors(n): prime = set() while n % 2 == 0: prime.add(2) n = n//2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: prime.add(i) n = n//i if n > 2: prime.add(n) return list(prime) def getFactors(n) : factors = [] i = 1 while i <= math.sqrt(n): if (n % i == 0) : if (n // i == i) : factors.append(i) else : factors.append(i) factors.append(n//i) i = i + 1 return factors def SieveOfEratosthenes(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 num = [] for p in range(2, n+1): if prime[p]: num.append(p) return num def lcm(a,b): return (a*b)//math.gcd(a,b) def sort_dict(key_value): return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]),reverse = True) def list_input(): return list(map(int,input().split())) def num_input(): return map(int,input().split()) def string_list(): return list(input()) def decimalToBinary(n): return bin(n).replace("0b", "") def binaryToDecimal(n): return int(n,2) def solve(): my_name = input() names = {} for _ in range(int(input())): s = input().split("'s") s = ''.join(s) s = s.split(' ') points = 0 if 'posted' == s[1]: n1,n2 = s[0],s[3] points = 15 elif 'commented' == s[1]: n1,n2 = s[0],s[3] points = 10 else: n1,n2 = s[0],s[2] points = 5 if n1 != my_name: try: names[n1] += points except KeyError: names[n1] = points if n2 != my_name: try: names[n2] += points except KeyError: names[n2] = points names = sort_dict(names) ans = [] i = 0 while i < len(names): u,v = names[i] j = i+1 temp = [u] while j < len(names) and v == names[j][1]: temp.append(names[j][0]) j += 1 i = j temp.sort() for t in temp: ans.append(t) for i in ans: print(i) #t = int(input()) t = 1 for _ in range(t): solve() ```
instruction
0
19,919
14
39,838
No
output
1
19,919
14
39,839
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` # x = {1 : 3, 7 : 9, 5 : 8} # x = sorted(x.items(), key=lambda x: x[1]) # for i in x : # print (i[0]) # print (x) import re d = dict () special = input () n = int (input ()) for i in range (n) : line = list (input ().split ()) first = line[0] order = line [1] second = line[-2] second = (re.search ('(.+)[\']', second))[1] if first == special or second == special : if order == 'posted' : d[first] = d.get (first, 0) + 15 d[second] = d.get (second, 0) + 15 elif order == 'commented' : d[first] = d.get (first, 0) + 10 d[second] = d.get (second, 0) + 10 else : d[first] = d.get (first, 0) + 5 d[second] = d.get (second, 0) + 5 else : d[first] = d.get (first, 0) d[second] = d.get (second, 0) d = sorted (d.items (), key = lambda x : x[1], reverse = True) zero = [] # print (d) for i in d : if i [0] != special and i[1] != 0: print (i[0]) elif i[1] == 0 : zero.append (i[0]) zero.sort () for i in zero : print (i) ```
instruction
0
19,920
14
39,840
No
output
1
19,920
14
39,841
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: * 1. "X posted on Y's wall" (15 points), * 2. "X commented on Y's post" (10 points), * 3. "X likes Y's post" (5 points). X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X). You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for any j (1 ≤ j < i) aj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Output fatma mona Input aba 1 likes likes posted's post Output likes posted Submitted Solution: ``` from collections import defaultdict s=str(input()) n=int(input()) d=defaultdict(int) for i in range(n): string=str(input()) l=string.split(" ") name1=l[0] if len(l)==5: name2=l[3][0:-2] else: name2=l[2][0:-2] factor=l[1] if factor=="posted": priority=15 if factor=="commented": priority=10 if factor=="likes": priority=5 if name1==s: d[name2]+=priority elif name2==s: d[name1]+=priority else: d[name1]=0 d[name2]=0 # print(d) d={k: v for k, v in sorted(d.items(), key=lambda item: item[1],reverse=True)} for i in d: print(i) ```
instruction
0
19,921
14
39,842
No
output
1
19,921
14
39,843
Provide tags and a correct Python 3 solution for this coding contest problem. Andryusha is an orderly boy and likes to keep things in their place. Today he faced a problem to put his socks in the wardrobe. He has n distinct pairs of socks which are initially in a bag. The pairs are numbered from 1 to n. Andryusha wants to put paired socks together and put them in the wardrobe. He takes the socks one by one from the bag, and for each sock he looks whether the pair of this sock has been already took out of the bag, or not. If not (that means the pair of this sock is still in the bag), he puts the current socks on the table in front of him. Otherwise, he puts both socks from the pair to the wardrobe. Andryusha remembers the order in which he took the socks from the bag. Can you tell him what is the maximum number of socks that were on the table at the same time? Input The first line contains the single integer n (1 ≤ n ≤ 105) — the number of sock pairs. The second line contains 2n integers x1, x2, ..., x2n (1 ≤ xi ≤ n), which describe the order in which Andryusha took the socks from the bag. More precisely, xi means that the i-th sock Andryusha took out was from pair xi. It is guaranteed that Andryusha took exactly two socks of each pair. Output Print single integer — the maximum number of socks that were on the table at the same time. Examples Input 1 1 1 Output 1 Input 3 2 1 1 3 2 3 Output 2 Note In the first example Andryusha took a sock from the first pair and put it on the table. Then he took the next sock which is from the first pair as well, so he immediately puts both socks to the wardrobe. Thus, at most one sock was on the table at the same time. In the second example Andryusha behaved as follows: * Initially the table was empty, he took out a sock from pair 2 and put it on the table. * Sock (2) was on the table. Andryusha took out a sock from pair 1 and put it on the table. * Socks (1, 2) were on the table. Andryusha took out a sock from pair 1, and put this pair into the wardrobe. * Sock (2) was on the table. Andryusha took out a sock from pair 3 and put it on the table. * Socks (2, 3) were on the table. Andryusha took out a sock from pair 2, and put this pair into the wardrobe. * Sock (3) was on the table. Andryusha took out a sock from pair 3 and put this pair into the wardrobe. Thus, at most two socks were on the table at the same time.
instruction
0
19,922
14
39,844
Tags: implementation Correct Solution: ``` tmp, _ = set(), input() print(max(((tmp.remove(x) if x in tmp else tmp.add(x)), len(tmp))[1] for x in map(int, input().split(' ')))) ```
output
1
19,922
14
39,845