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. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,324
14
78,648
Tags: dfs and similar, graphs, trees Correct Solution: ``` n=int(input()) a=[0]+[1]*n p = list(map(int,input().split())) nei=[[] for i in range(n+1)] nei[0]=[1] for i,e in enumerate(p): nei[e]+=[i+2] d=[-1]*(n+1) vert=[0] index=0 while index<=n: v=vert[index] for nnn in nei[v]: d[nnn]=d[v]+1 vert.append(nnn) index+=1 from collections import Counter cc=Counter(d) s=0 for i,e in cc.items(): s+=e%2 print(s-1) ```
output
1
39,324
14
78,649
Provide tags and a correct Python 3 solution for this coding contest problem. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,325
14
78,650
Tags: dfs and similar, graphs, trees Correct Solution: ``` n = int(input()) dp = dict() cnt = dict() dp[1] = 1 cnt[1] = 1 p = list(map(int, input().split())) for i in range(0, n-1): dp[i+2] = dp[p[i]]+1 if dp[i+2] in cnt: cnt[dp[i+2]]+=1 else: cnt[dp[i+2]] = 1 ans = 0 for k, v in cnt.items(): if v%2 == 1: ans+=1 print(ans) ```
output
1
39,325
14
78,651
Provide tags and a correct Python 3 solution for this coding contest problem. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,326
14
78,652
Tags: dfs and similar, graphs, trees Correct Solution: ``` R = lambda : map(int, input().split()) n = int(input()) v = list(R()) adj = [[] for _ in range(n)] r = [0]*n for i in range(n-1): adj[v[i]-1].append(i+1) from collections import deque q = deque() q.append((0,0)) while q: v=q.popleft() r[v[1]]+=1 for nv in adj[v[0]]: q.append((nv,v[1]+1)) res = [1 for x in r if x%2==1] print(sum(res)) ```
output
1
39,326
14
78,653
Provide tags and a correct Python 3 solution for this coding contest problem. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,327
14
78,654
Tags: dfs and similar, graphs, trees Correct Solution: ``` n=int(input()) le=[0]*(n+1) le[1]=1 l=[int(x) for x in input().split()] for i in range(len(l)): le[i+2]=le[l[i]]+1 f=[0]*(n+1) #print(le) for ele in le: f[ele]+=1 ans=0 #print(f) for ele in f: if ele%2==1: ans+=1 print(ans-1) ```
output
1
39,327
14
78,655
Provide tags and a correct Python 3 solution for this coding contest problem. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,328
14
78,656
Tags: dfs and similar, graphs, trees Correct Solution: ``` tree = [] class Node: def __init__(self, num): self.parent = num - 1 self.length = 0 def add_to_tree(self): if self.parent < 0: return self.length = tree[self.parent].length + 1 def main(): n = int(input()) global tree tree = [Node(0)] + [Node(int(x)) for x in input().split()] for i in tree: i.add_to_tree() amount = [0] * n for i in [x.length for x in tree]: amount[i] += 1 print(sum([x & 1 for x in amount])) if __name__ == "__main__": main() ```
output
1
39,328
14
78,657
Provide tags and a correct Python 3 solution for this coding contest problem. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,329
14
78,658
Tags: dfs and similar, graphs, trees Correct Solution: ``` n = int(input()) pi = list(map(int, input().split())) ar = [0] * n ar2 = [0] * n ar2[0] = 1 for i in range(n-1): temp = ar[pi[i]-1] + 1 ar[i+1] = temp ar2[temp] += 1 ans = 0 for i in range(n): if ar2[i] % 2 == 1: ans += 1 print(ans) ```
output
1
39,329
14
78,659
Provide tags and a correct Python 3 solution for this coding contest problem. In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are n inflorescences, numbered from 1 to n. Inflorescence number 1 is situated near base of tree and any other inflorescence with number i (i > 1) is situated at the top of branch, which bottom is pi-th inflorescence and pi < i. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in a-th inflorescence gets to pa-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number n (2 ≤ n ≤ 100 000) — number of inflorescences. Second line of input contains sequence of n - 1 integer numbers p2, p3, ..., pn (1 ≤ pi < i), where pi is number of inflorescence into which the apple from i-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 3 1 1 Output 1 Input 5 1 2 2 2 Output 3 Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Output 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
instruction
0
39,330
14
78,660
Tags: dfs and similar, graphs, trees Correct Solution: ``` from collections import Counter n = int(input()) p = list(map(int, input().split())) co = [1] for i in range(n - 1): co.append(co[p[i] - 1] + 1) ans = 0 for v in Counter(co).values(): ans += v % 2 print(ans) ```
output
1
39,330
14
78,661
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,398
14
78,796
"Correct Solution: ``` from itertools import product N = int(input()) B = list(product([1, 0], repeat=N)) L = [] for i in range(N): A = int(input()) for j in range(A): x, y = map(int, input().split()) L.append([x, y, i+1]) ans = 0 for b in B: for l in L: if b[l[2]-1] == 1 and l[1] != b[l[0]-1]: break else: if ans < b.count(1): ans = b.count(1) print(ans) ```
output
1
39,398
14
78,797
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,399
14
78,798
"Correct Solution: ``` import itertools N = int(input()) T = [[] for _ in range(N)] for i in range(N): for _ in range(int(input())): x, y = map(int, input().split()) T[i].append([x - 1, y]) ans = 0 for bit in list(itertools.product([0, 1], repeat=N)): ok = True for i in range(N): if bit[i]: for x, y in T[i]: if bit[x] != y: ok = False if ok: ans = max(ans, sum(bit)) print(ans) ```
output
1
39,399
14
78,799
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,400
14
78,800
"Correct Solution: ``` import itertools N=int(input()) l=[[] for i in range(N)] for i in range(N): a=int(input()) for j in range(a): x,y=map(int,input().split()) l[i].append((x-1,y)) ans=0 for t in itertools.product(range(2),repeat=N): miss=False for i in range(N): if t[i]==0:continue for x,y in l[i]: if not t[x]==y:break else:continue break else:ans=max(ans,sum(t)) print(ans) ```
output
1
39,400
14
78,801
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,401
14
78,802
"Correct Solution: ``` n = int(input()) l = [] for i in range(n): a = int(input()) l.append([tuple(map(int, input().split())) for _ in range(a)]) cnt = 0 for i in range(1 << n): Flag = True for j in range(n): if (i >> j) & 1: for x, y in l[j]: if i >> (x-1) & 1 != y: Flag = False break if Flag: cnt = max(cnt, sum(map(int, bin(i)[2:]))) print(cnt) ```
output
1
39,401
14
78,803
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,402
14
78,804
"Correct Solution: ``` n=int(input()) s=[] ans=0 for i in range(n): a=int(input()) s.append([list(map(int,input().split())) for _ in range(a)]) for i in range(2**n): bit=bin(i)[2:].zfill(n) flag=1 for j in range(n): if bit[j]=='0': continue for sh in s[j]: if int(bit[sh[0]-1])!=sh[1]: flag=0 if flag: ans=max(ans,bit.count('1')) print(ans) ```
output
1
39,402
14
78,805
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,403
14
78,806
"Correct Solution: ``` n=int(input()) L=[[list(map(int,input().split())) for _ in range(int(input()))] for _ in range(n)] a=0 for i in range(1, 2**n): f=True for j in range(n): if (i>>j)&1==0: continue for x,y in L[j]: if (i>>(x-1))&1!=y: f=False break if not f: break if f: a=max(a, bin(i)[2:].count('1')) print(a) ```
output
1
39,403
14
78,807
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,404
14
78,808
"Correct Solution: ``` N = int(input()) lst = [] for i in range(N): a = int(input()) xy = [[int(x) for x in input().split()] for _ in range(a)] lst.append(xy) ans = 0 for i in range(1, 2**N): f = 1 tmp = [i >> j & 1 for j in range(N)] cnt = sum(tmp) for i, yi in enumerate(tmp): if (yi == 0): continue if any([tmp[x-1] != y for x, y in lst[i]]): f = 0 break if (f): ans = max(cnt, ans) print(ans) ```
output
1
39,404
14
78,809
Provide a correct Python 3 solution for this coding contest problem. There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not. Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind. How many honest persons can be among those N people at most? Constraints * All values in input are integers. * 1 \leq N \leq 15 * 0 \leq A_i \leq N - 1 * 1 \leq x_{ij} \leq N * x_{ij} \neq i * x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2) * y_{ij} = 0, 1 Input Input is given from Standard Input in the following format: N A_1 x_{11} y_{11} x_{12} y_{12} : x_{1A_1} y_{1A_1} A_2 x_{21} y_{21} x_{22} y_{22} : x_{2A_2} y_{2A_2} : A_N x_{N1} y_{N1} x_{N2} y_{N2} : x_{NA_N} y_{NA_N} Output Print the maximum possible number of honest persons among the N people. Examples Input 3 1 2 1 1 1 1 1 2 0 Output 2 Input 3 2 2 1 3 0 2 3 1 1 0 2 1 1 2 0 Output 0 Input 2 1 2 0 1 1 0 Output 1
instruction
0
39,405
14
78,810
"Correct Solution: ``` import itertools n = int(input()) data = [] for x in range(n): for y in range(int(input())): k, l = map(int, input().split()) data.append((x, k - 1, l)) ans = 0 for i in itertools.product([1,0], repeat=n): s = sum(list(i)) if ans > s: continue for g in data: if i[g[0]] == 1 and i[g[1]] != g[2]: s = 0 break ans = max(ans, s) print(ans) ```
output
1
39,405
14
78,811
Provide a correct Python 3 solution for this coding contest problem. Mr. Dango's family has extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in near future. They all have warm and gracious personality and are close each other. They usually communicate by a phone. Of course, They are all taking a family plan. This family plan is such a thing: when a choose b, and b choose a as a partner, a family plan can be applied between them and then the calling fee per unit time between them discounted to f(a, b), which is cheaper than a default fee. Each person can apply a family plan at most 2 times, but no same pair of persons can apply twice. Now, choosing their partner appropriately, all members of Mr. Dango's family applied twice. Since there are huge number of people, it is very difficult to send a message to all family members by a phone call. Mr. Dang have decided to make a phone calling network that is named 'clan' using the family plan. Let us present a definition of clan. Let S be an any subset of all phone calls that family plan is applied. Clan is S such that: 1. For any two persons (let them be i and j), if i can send a message to j through phone calls that family plan is applied (directly or indirectly), then i can send a message to j through only phone calls in S (directly or indirectly). 2. Meets condition 1 and a sum of the calling fee per unit time in S is minimized. Clan allows to send a message efficiently. For example, we suppose that one have sent a message through all calls related to him in the clan. Additionaly we suppose that every people follow a rule, "when he/she receives a message through a call in clan, he/she relays the message all other neibors in respect to clan." Then, we can prove that this message will surely be derivered to every people that is connected by all discounted calls, and that the message will never be derivered two or more times to same person. By the way, you are given information about application of family plan of Mr. Dango's family. Please write a program that calculates that in how many ways a different clan can be constructed. You should output the answer modulo 10007 because it may be very big. Constraints * 3 ≤ n ≤ 100,000 Input The input consists of several datasets. The first line of each dataset contains an integer n, which indicates the number of members in the family. Next n lines represents information of the i-th member with four integers. The first two integers respectively represent b[0] (the partner of i) and f(i, b[0]) (the calling fee per unit time between i and b[0]). The following two integers represent b[1] and f(i, b[1]) in the same manner. Input terminates with a dataset where n = 0. Output For each dataset, output the number of clan modulo 10007. Example Input 3 1 1 2 3 0 1 2 2 1 2 0 3 7 1 2 2 1 0 2 3 2 0 1 3 1 2 1 1 2 5 3 6 2 4 3 6 1 4 2 5 1 0 Output 1 2
instruction
0
39,555
14
79,110
"Correct Solution: ``` while True: n=int(input()) if n==0: break g=[[] for _ in range(n)] for u in range(n): v1,w1,v2,w2=map(int,input().split()) g[u].append((v1,w1)) g[u].append((v2,w2)) res=1 vis=[False]*n for i in range(n): if vis[i]: continue ws=[] p,u=-1,i while True: vis[u]=True for v,w in g[u]: if v!=p: ws.append(w) p,u=u,v break if u==i: break res=res*ws.count(max(ws))%10007 print(res) ```
output
1
39,555
14
79,111
Provide a correct Python 3 solution for this coding contest problem. Mr. Dango's family has extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in near future. They all have warm and gracious personality and are close each other. They usually communicate by a phone. Of course, They are all taking a family plan. This family plan is such a thing: when a choose b, and b choose a as a partner, a family plan can be applied between them and then the calling fee per unit time between them discounted to f(a, b), which is cheaper than a default fee. Each person can apply a family plan at most 2 times, but no same pair of persons can apply twice. Now, choosing their partner appropriately, all members of Mr. Dango's family applied twice. Since there are huge number of people, it is very difficult to send a message to all family members by a phone call. Mr. Dang have decided to make a phone calling network that is named 'clan' using the family plan. Let us present a definition of clan. Let S be an any subset of all phone calls that family plan is applied. Clan is S such that: 1. For any two persons (let them be i and j), if i can send a message to j through phone calls that family plan is applied (directly or indirectly), then i can send a message to j through only phone calls in S (directly or indirectly). 2. Meets condition 1 and a sum of the calling fee per unit time in S is minimized. Clan allows to send a message efficiently. For example, we suppose that one have sent a message through all calls related to him in the clan. Additionaly we suppose that every people follow a rule, "when he/she receives a message through a call in clan, he/she relays the message all other neibors in respect to clan." Then, we can prove that this message will surely be derivered to every people that is connected by all discounted calls, and that the message will never be derivered two or more times to same person. By the way, you are given information about application of family plan of Mr. Dango's family. Please write a program that calculates that in how many ways a different clan can be constructed. You should output the answer modulo 10007 because it may be very big. Constraints * 3 ≤ n ≤ 100,000 Input The input consists of several datasets. The first line of each dataset contains an integer n, which indicates the number of members in the family. Next n lines represents information of the i-th member with four integers. The first two integers respectively represent b[0] (the partner of i) and f(i, b[0]) (the calling fee per unit time between i and b[0]). The following two integers represent b[1] and f(i, b[1]) in the same manner. Input terminates with a dataset where n = 0. Output For each dataset, output the number of clan modulo 10007. Example Input 3 1 1 2 3 0 1 2 2 1 2 0 3 7 1 2 2 1 0 2 3 2 0 1 3 1 2 1 1 2 5 3 6 2 4 3 6 1 4 2 5 1 0 Output 1 2
instruction
0
39,556
14
79,112
"Correct Solution: ``` # AOJ 1055 Huge Family # Python3 2018.7.7 bal4u # UNION-FIND library class UnionSet: def __init__(self, nmax): self.size = [1]*nmax self.id = [i for i in range(nmax+1)] def root(self, i): while i != self.id[i]: self.id[i] = self.id[self.id[i]] i = self.id[i] return i def connected(self, p, q): return self.root(p) == self.root(q) def unite(self, p, q): i, j = self.root(p), self.root(q) if i == j: return if self.size[i] < self.size[j]: self.id[i] = j self.size[j] += self.size[i] else: self.id[j] = i self.size[i] += self.size[j] # UNION-FIND library def check(k, f): if cnt[k] == 0 or f > vmax[k]: cnt[k], vmax[k] = 1, f elif f == vmax[k]: cnt[k] += 1 while True: n = int(input()) if n == 0: break u = UnionSet(n) f = [[0 for j in range(2)] for i in range(n)] cnt, vmax = [0]*n, [0]*n for i in range(n): a, f[i][0], b, f[i][1] = map(int, input().split()) u.unite(i, a) u.unite(i, b) for i in range(n): k = u.root(i) check(k, f[i][0]) check(k, f[i][1]) ans = 1 for i in range(n): if cnt[i]: ans = ans * (cnt[i] >> 1) % 10007 print(ans) ```
output
1
39,556
14
79,113
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mr. Dango's family has extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in near future. They all have warm and gracious personality and are close each other. They usually communicate by a phone. Of course, They are all taking a family plan. This family plan is such a thing: when a choose b, and b choose a as a partner, a family plan can be applied between them and then the calling fee per unit time between them discounted to f(a, b), which is cheaper than a default fee. Each person can apply a family plan at most 2 times, but no same pair of persons can apply twice. Now, choosing their partner appropriately, all members of Mr. Dango's family applied twice. Since there are huge number of people, it is very difficult to send a message to all family members by a phone call. Mr. Dang have decided to make a phone calling network that is named 'clan' using the family plan. Let us present a definition of clan. Let S be an any subset of all phone calls that family plan is applied. Clan is S such that: 1. For any two persons (let them be i and j), if i can send a message to j through phone calls that family plan is applied (directly or indirectly), then i can send a message to j through only phone calls in S (directly or indirectly). 2. Meets condition 1 and a sum of the calling fee per unit time in S is minimized. Clan allows to send a message efficiently. For example, we suppose that one have sent a message through all calls related to him in the clan. Additionaly we suppose that every people follow a rule, "when he/she receives a message through a call in clan, he/she relays the message all other neibors in respect to clan." Then, we can prove that this message will surely be derivered to every people that is connected by all discounted calls, and that the message will never be derivered two or more times to same person. By the way, you are given information about application of family plan of Mr. Dango's family. Please write a program that calculates that in how many ways a different clan can be constructed. You should output the answer modulo 10007 because it may be very big. Constraints * 3 ≤ n ≤ 100,000 Input The input consists of several datasets. The first line of each dataset contains an integer n, which indicates the number of members in the family. Next n lines represents information of the i-th member with four integers. The first two integers respectively represent b[0] (the partner of i) and f(i, b[0]) (the calling fee per unit time between i and b[0]). The following two integers represent b[1] and f(i, b[1]) in the same manner. Input terminates with a dataset where n = 0. Output For each dataset, output the number of clan modulo 10007. Example Input 3 1 1 2 3 0 1 2 2 1 2 0 3 7 1 2 2 1 0 2 3 2 0 1 3 1 2 1 1 2 5 3 6 2 4 3 6 1 4 2 5 1 0 Output 1 2 Submitted Solution: ``` while True: n=int(input()) if n==0: break g=[[] for _ in range(n)] for u in range(n): v1,w1,v2,w2=map(int,input().split()) g[u].append((v1,w1)) g[u].append((v2,w2)) res=1 vis=[False]*n for i in range(n): if vis[i]: continue ws=[] p,u=-1,i while True: vis[u]=True for v,w in g[u]: if v!=p: ws.append(w) p,u=u,v break if u==i: break res*=ws.count(max(ws)) print(res) ```
instruction
0
39,557
14
79,114
No
output
1
39,557
14
79,115
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,616
14
79,232
Tags: greedy, implementation, math Correct Solution: ``` r=input;r();print(sum(int(v)*i-i+1for i,v in enumerate(r().split(),1))) ```
output
1
39,616
14
79,233
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,617
14
79,234
Tags: greedy, implementation, math Correct Solution: ``` n = int(input()) l = list(map(int,input().split())) if len(set(l)) == 1 and l[0] == 1 or n == 1 : print(sum(l)) else: cnt = l[0] for i in range(1 , n): cnt += l[i] + (l[i]-1) * i print(cnt) ```
output
1
39,617
14
79,235
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,618
14
79,236
Tags: greedy, implementation, math Correct Solution: ``` n = int(input().strip()) answers = [int(ele) for ele in input().strip().split()] total_clicks = 0 for i in range(n): num_wrongs = answers[i] - 1 total_clicks += num_wrongs*(i+1) + 1 print(total_clicks) ```
output
1
39,618
14
79,237
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,619
14
79,238
Tags: greedy, implementation, math Correct Solution: ``` n = int(input()) l = [int(x) for x in input().split()] sum = l[0] for i in range(1,n): sum += 1+(l[i]-1)*(i+1) print(sum) ```
output
1
39,619
14
79,239
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,620
14
79,240
Tags: greedy, implementation, math Correct Solution: ``` n = int(input()) A = list(map(int, input().split())) result = 0 for i in range(n): result += (A[i] - 1) * (i + 1) + 1 print(result) ```
output
1
39,620
14
79,241
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,621
14
79,242
Tags: greedy, implementation, math Correct Solution: ``` class CodeforcesTask104BSolution: def __init__(self): self.result = '' self.n = 0 self.questions = [] def read_input(self): self.n = int(input()) self.questions = [int(x) for x in input().split(" ")] def process_task(self): wrong_click_cost = [x for x in range(self.n)] cost = 0 for x in range(self.n): cost += 1 + (self.questions[x] - 1) * (1 + wrong_click_cost[x]) self.result = str(cost) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask104BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
output
1
39,621
14
79,243
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,622
14
79,244
Tags: greedy, implementation, math Correct Solution: ``` def arr_inp(): return [int(x) for x in stdin.readline().split()] from sys import * n, a = int(input()), arr_inp() print(sum([a[i] + (a[i] - 1) * i for i in range(n)])) ```
output
1
39,622
14
79,245
Provide tags and a correct Python 3 solution for this coding contest problem. The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct. A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves. Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test's theme. How many clicks will he have to perform in the worst case? Input The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i. Output Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 2 1 1 Output 2 Input 2 2 2 Output 5 Input 1 10 Output 10 Note Note to the second sample. In the worst-case scenario you will need five clicks: * the first click selects the first variant to the first question, this answer turns out to be wrong. * the second click selects the second variant to the first question, it proves correct and we move on to the second question; * the third click selects the first variant to the second question, it is wrong and we go back to question 1; * the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question; * the fifth click selects the second variant to the second question, it proves correct, the test is finished.
instruction
0
39,623
14
79,246
Tags: greedy, implementation, math Correct Solution: ``` r=input;r();print(sum(int(v)*i-i+1for i,v in enumerate(r().split(),1))) # Made By Mostafa_Khaled ```
output
1
39,623
14
79,247
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,736
14
79,472
Tags: constructive algorithms, data structures, math Correct Solution: ``` n=int(input()) a=[*map(int,input().split())] b=[1]*(n+1) r=range(n) for i in r:b[a[i]]=0 s={i for i in r if b[i+1]} t=[i for i in s if a[i]<1] for i,j in zip(t,t[1:]+t[:1]): if i^j:a[i]=j+1;s-={i} t+=r for i in t: if a[i]<1: j=s.pop() if j==i:j=s.pop();s|={i} a[i]=j+1 print(*a) ```
output
1
39,736
14
79,473
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,737
14
79,474
Tags: constructive algorithms, data structures, math Correct Solution: ``` n = int(input()) want = list(map(int, input().split())) st = set(range(1, n + 1)) for f in want: if f != 0: st.remove(f) st = list(st) for i in range(n): if len(st) == 2: break if want[i] == 0: if st[-1] == i + 1: st[-1], st[-2] = st[-2], st[-1] pp = st[-1] want[i] = pp st.pop() i1 = -1 i2 = -1 for i in range(n): if want[i] == 0: if i1 == -1: i1 = i else: i2 = i break if i1 + 1 == st[0] or i2 + 1 == st[1]: i1, i2 = i2, i1 want[i1] = st[0] want[i2] = st[1] print(' '.join(map(str, want))) ```
output
1
39,737
14
79,475
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,738
14
79,476
Tags: constructive algorithms, data structures, math Correct Solution: ``` def checkSame(notGet, notGive): for index in range(len(notGet)): if notGet[index] == notGive[index]: return True return False totalCase = input() inputNumber = input() newNumber = inputNumber.split() getting = {} notGive = [] notGet = [] for i in range(int(totalCase)): if int(newNumber[i]) != 0: getting[newNumber[i]] = True else: notGive.append(i+1) for i in range(int(totalCase)): if str(i+1) not in getting: notGet.append(i+1) checkFlag = True while checkFlag: temp = notGet[0] notGet = notGet[1:len(notGet)] notGet.append(temp) checkFlag = checkSame(notGet, notGive) indexGet = 0 for index in range(int(totalCase)): if int(newNumber[index]) == 0: newNumber[index] = str(notGet[indexGet]) indexGet = indexGet+1 delim = ' ' print(delim.join(newNumber)) ```
output
1
39,738
14
79,477
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,739
14
79,478
Tags: constructive algorithms, data structures, math Correct Solution: ``` n=int(input()) l2=[int(a) for a in input().split()] l3=[] l4=[] for i in range(n+1): l3.append(0) for i in range(n): l3[l2[i]]=1 for i in range(1,n+1): if(l3[i]==0): l4.append(i) c=0 a2=0 a1=0 for i in range(n): if(l2[i]==0): if(i+1==l4[c]): if(c!=len(l4)-1): a1=l4[c] l4[c]=l4[c+1] l4[c+1]=a1 else: a1=l4[c-1] l4[c-1]=l4[c] l4[c]=a1 l2[a2]=l4[c-1] l2[i]=l4[c] a2=i c+=1 else: l2[i]=l4[c] a2=i c+=1 else: pass for i in range(n): print(l2[i],end=" ") ```
output
1
39,739
14
79,479
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,740
14
79,480
Tags: constructive algorithms, data structures, math Correct Solution: ``` def inp(dtype=str, strip=True): s = input() res = [dtype(p) for p in s.split()] res = res[0] if len(res) == 1 and strip else res return res def problem1(): t = inp(int) for _ in range(t): h, m = inp(int) print((23 - h) * 60 + (60 - m)) def problem2(): t = inp(int) for _ in range(t): n, k = inp(int) res = (n // k) * k + min(n % k, k // 2) print(res) def problem3(): n = inp(int) f = inp(int, strip=False) f = [ch - 1 for ch in f] g = [-1 for _ in range(n)] for i, ch in enumerate(f): if ch >= 0: g[ch] = i gave = set([i for i in range(n) if f[i] >= 0 and g[i] == -1]) received = set([i for i in range(n) if f[i] == -1 and g[i] >= 0]) neither = set([i for i in range(n) if f[i] == -1 and g[i] == -1]) # give a gift to anyone who has not yet given one # and has not yet received one while len(neither): # i is giving to j if len(received): i = received.pop() else: i = neither.pop() gave.add(i) j = neither.pop() received.add(j) f[i] = j while len(received): i = received.pop() j = gave.pop() f[i] = j f = [ch + 1 for ch in f] print(' '.join([str(ch) for ch in f])) def problem4(): pass def problem5(): pass if __name__ == '__main__': # problem1() # problem2() problem3() # problem4() # problem5() ```
output
1
39,740
14
79,481
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,741
14
79,482
Tags: constructive algorithms, data structures, math Correct Solution: ``` if __name__ == '__main__': input() giving = list(map(lambda x: x - 1, map(int, input().split()))) not_giving = [i for i, x in enumerate(giving) if x < 0] first_not_giver = not_giving[0] receiving = [False for _ in range(len(giving))] for x in giving: if x >= 0: receiving[x] = True not_receiving = set() for i, b in enumerate(receiving): if not b: not_receiving.add(i) for x in not_giving: y = not_receiving.pop() if x == y: if not_receiving: tmp = not_receiving.pop() giving[x] = tmp not_receiving.add(y) else: tmp = giving[first_not_giver] giving[first_not_giver] = y giving[x] = tmp else: giving[x] = y print(*map(lambda x: x + 1, giving)) ```
output
1
39,741
14
79,483
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,742
14
79,484
Tags: constructive algorithms, data structures, math Correct Solution: ``` n = int(input()) l = list(map(int,input().split())) dostal = [0] * (n+1) for i in range(n): dostal[l[i]] = 1 do_dania = [] ind = [] for i in range(n): if l[i] == 0: ind.append(i) for i in range(1, n+1): if dostal[i] == 0: do_dania.append(i) #print(do_dania) j = 0 for i in range(n): if l[i] == 0: l[i] = do_dania[j] j += 1 for i in range(len(ind) - 1): if l[ind[i]] == ind[i] + 1: kk = l[ind[i]] l[ind[i]] = l[ind[i+1]] l[ind[i+1]] = kk if l[ind[-1]] == ind[-1] + 1: kk = l[ind[-1]] l[ind[-1]] = l[ind[0]] l[ind[0]] = kk print(*l) ```
output
1
39,742
14
79,485
Provide tags and a correct Python 3 solution for this coding contest problem. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3
instruction
0
39,743
14
79,486
Tags: constructive algorithms, data structures, math Correct Solution: ``` n=int(input()) f=[int(i) for i in input().split()] m=dict() for i in range(n): m[f[i]]=1 l=list() for i in range(1,n+1): if m.get(i): continue else: l.insert(len(l),i) l.sort(reverse=True) val=-1 pre=[] nex=[] for i in range(n): pre.insert(len(pre),0);nex.insert(len(nex),0) for i in range(n-1,-1,-1): if not f[i]: nex[n-1-i]=val val=i nex.reverse() val=-1 prev=0 for i in range(n): if not f[i]: pre[i]=val val=i f[i]=l[prev] prev+=1 for i in range(n): if f[i] == i+1: if pre[i]==-1: idx=nex[i] else: idx=pre[i] tmp=f[idx] f[idx]=f[i] f[i]=tmp for i in range(n): print(f[i],end=' ') print() ```
output
1
39,743
14
79,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` import sys def fastio(): from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() def debug(*var, sep = ' ', end = '\n'): print(*var, file=sys.stderr, end = end, sep = sep) INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br n, = I() f = [0] + I() v = [0] * (n + 1) for i in f: v[i] = 1 not_decided = [] not_visited = [] for i in range(1, n+1): if f[i] == 0: not_decided.append(i) if v[i] == 0: not_visited.append(i) k = len(not_decided) ok = set(not_visited) common = [] un = [] for i in range(k): if not_decided[i] in ok: common.append(not_decided[i]) else: un.append(not_decided[i]) not_decided = common + un # print(not_visited, not_decided) for i in range(k): x = not_decided[i] y = not_visited[i] if x == y and i + 1 < k: not_visited[i], not_visited[i + 1] = not_visited[i+1], not_visited[i] elif x == y: # print(not_decided, not_visited, not_visited[i-1], i) f[not_decided[i-1]] = not_visited[i] f[not_decided[i]] = not_visited[i-1] break f[x] = not_visited[i] print(*f[1:]) ```
instruction
0
39,744
14
79,488
Yes
output
1
39,744
14
79,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` #----------------------------------------------------------------------- # Richard Mello # C - Friends and Gifts #----------------------------------------------------------------------- # Recebe n = int(input()) ganhou = [0] + [int(i) for i in input().split()] conjunto = set(ganhou) # Vê quem tá sem e quem tá indeciso sem = [] indecisos = [] rep = [] nrep = [] for i in range(1, n+1): # Se não recebeu if i not in conjunto: # Se repetiu if ganhou[i] == 0: rep.append(i) # Senão else: sem.append(i) # Se tá indeciso mas não repetiu elif ganhou[i] == 0: nrep.append(i) # Trata dos repetidos t = len(rep) if t > 0: # Caso em que só tem 1 if t == 1: rep.append(nrep[-1]) del(nrep[-1]) for i in range(0, t-1): ganhou[rep[i]] = rep[i+1] ganhou[rep[-1]] = rep[0] # Trata dos outros pos = 0 for i in range(1, n+1): # Se o cara ainda for 0 if ganhou[i] == 0: # Adiciona ganhou[i] = sem[pos] pos += 1 # Mostra for i in range(1, n+1): print(ganhou[i], end = ' ') print() #----------------------------------------------------------------------- ```
instruction
0
39,745
14
79,490
Yes
output
1
39,745
14
79,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` import sys input = sys.stdin.readline n=int(input()) F=list(map(int,input().split())) USE=[0]*(n+1) B=[] for i in range(n): USE[F[i]]=1 if F[i]==0: B.append(i+1) A=[] for i in range(1,n+1): if USE[i]==0: A.append(i) for i in range(len(A)-1): if A[i]==B[i]: A[i],A[i+1]=A[i+1],A[i] if A[-1]==B[-1]: A[-1],A[-2]=A[-2],A[-1] ind=0 for i in range(n): if F[i]==0: F[i]=A[ind] ind+=1 print(*F) ```
instruction
0
39,746
14
79,492
Yes
output
1
39,746
14
79,493
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` n=int(input()) l=list(map(int,input().split())) l1=[0]*(n+1) for i in range(n): l1[l[i]]=1 l2=[] for i in range(1,n+1): if(l1[i]==0): l2.append(i) l3=[] for i in range(n): if(l[i]==0): l3.append(i+1) if(len(l3)==1): for i in range(n): if(l[i]==0): l[i]=l2[0] else: t=0 for i in range(0,len(l3)-1): if(l2[i]==l3[i] or l2[i+1]==l3[i+1]): temp=l2[i] l2[i]=l2[i+1] l2[i+1]=temp for i in range(n): if(l[i]==0): l[i]=l2[t] t+=1 for i in range(n): print(l[i],end=" ") print() ```
instruction
0
39,747
14
79,494
Yes
output
1
39,747
14
79,495
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` prev=-1 n=int(input()) f=list(map(int, input().split())) given=[] notgiven=[] for i in range(n): if f[i]!=0: given.append(f[i]) given.sort() j=1 for i in range(len(given)): while j<given[i]: notgiven.append(j) j+=1 j+=1 while j<=n: notgiven.append(j) j+=1 for i in range(n): if f[i]==0: if prev==-1: f[i]=notgiven.pop(-2) else: f[i]=notgiven.pop() if i+1==f[i]: temp=f[prev] f[prev]=f[i] f[i]=temp prev=i for fi in f: print (fi, end=' ') ```
instruction
0
39,748
14
79,496
No
output
1
39,748
14
79,497
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) uns=[True]*(n*10) extra=set() r=set() for i in range(n): if a[i]!=0: uns[a[i]]=False else: extra.add(i+1) #print(uns,extra) priority=[] flag=True for i in range(1,n+1): if uns[i] and i in extra: priority.append((1,i)) r.add(i) elif uns[i]: flag=False priority.append((0,i)) priority.sort() ultraflag=False ul=0 ur=0 if flag and len(priority)%2!=0: ul=priority[0][1] ur=priority[-1][1] ultraflag=True #print(priority) while priority: if len(priority)>=2: t1=priority.pop() t2=priority.pop() if t1[0]==1 and t2[0]==1: a[t1[1]-1]=t2[1] a[t2[1]-1]=t1[1] if t1[1] in extra: extra.remove(t1[1]) if t2[1] in extra: extra.remove(t2[1]) elif t1[0]==0 and t2[0]==0: e1=e2=0 if len(extra)>0: e1=extra.pop() if len(extra)>0: e2=extra.pop() a[e1-1]=t1[1] a[e2-1]=t2[1] else: e1=extra.pop() if t1[0]==1: if t1[1] in extra: extra.remove(t1[1]) a[t1[1]-1]=t2[1] a[e1-1]=t1[1] else: if t2[1] in extra: extra.remove(t2[1]) a[t2[1]-1]=t1[1] a[e1-1]=t2[1] else: t1=priority.pop() e1=extra.pop() a[e1-1]=t1[1] if ultraflag and a[e1-1]==e1: j=t1[1] while j==t1[1] and a[j-1]==t1[1]: j=r.pop() temp=a[j-1] a[j-1]=t1[1] a[t1[1]-1]=temp print(*a) ```
instruction
0
39,749
14
79,498
No
output
1
39,749
14
79,499
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` from collections import defaultdict def solve(n, f): gives = defaultdict() receives = defaultdict() for i, v in enumerate(f): if v != 0: gives[i] = v - 1 receives[v - 1] = i not_received = set(list(range(n))) - set(receives.keys()) not_given = set(list(range(n))) - set(gives.keys()) inte = not_received & not_given s = [] for i, v in enumerate(f): if v != 0: s.append(v) else: if inte: x = inte.pop() if x + 1 != i + 1: s.append(x + 1) else: if inte: y = inte.pop() s.append(y + 1) inte.add(x) else: y = not_received.pop() s.append(y + 1) inte.add(x) else: a = not_received.pop() if a + 1 != i + 1: s.append(a + 1) else: if inte: y = inte.pop() s.append(y + 1) inte.add(a) else: b = not_received.pop() s.append(b + 1) not_received.add(a) return ' '.join(map(str, s)) def main(): n = int(input()) f = list(map(int, input().split())) print(solve(n, f)) main() ```
instruction
0
39,750
14
79,500
No
output
1
39,750
14
79,501
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value f_i is known: it is either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. You want to fill in the unknown values (f_i = 0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of friends. The second line of the input contains n integers f_1, f_2, ..., f_n (0 ≤ f_i ≤ n, f_i ≠ i, all f_i ≠ 0 are distinct), where f_i is the either f_i = 0 if the i-th friend doesn't know whom he wants to give the gift to or 1 ≤ f_i ≤ n if the i-th friend wants to give the gift to the friend f_i. It is also guaranteed that there is at least two values f_i = 0. Output Print n integers nf_1, nf_2, ..., nf_n, where nf_i should be equal to f_i if f_i ≠ 0 or the number of friend whom the i-th friend wants to give the gift to. All values nf_i should be distinct, nf_i cannot be equal to i. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any. Examples Input 5 5 0 0 2 4 Output 5 3 1 2 4 Input 7 7 0 0 1 4 0 6 Output 7 3 2 1 4 5 6 Input 7 7 4 0 3 0 5 1 Output 7 4 2 3 6 5 1 Input 5 2 1 0 0 0 Output 2 1 4 5 3 Submitted Solution: ``` from collections import deque n=int(input()) s=deque() a=list(map(int,input().split())) b=set(a) b.remove(0) for i in range(len(a)): if i+1 in b: continue else: s.append(i+1) for item in range(len(a)): if a[item]!=0: continue else: if item+1==s[0]: a[item]=s.pop() else: a[item]=s.popleft() print(*a) ```
instruction
0
39,751
14
79,502
No
output
1
39,751
14
79,503
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,939
14
79,878
Tags: implementation Correct Solution: ``` R = lambda:map(int, input().split()) n, m, k = R() a = [list(R()) for _ in range(n)] b = [[0] * m for _ in range(n)] c = [0] * m for i in range(k): x, y = R() x -= 1 y -= 1 c[y] += 1 b[x][y] += 1 print(" ".join(map(str, (sum(c[j] - b[i][j] for j in range(m) if a[i][j] == 1) for i in range(n))))) ```
output
1
39,939
14
79,879
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,940
14
79,880
Tags: implementation Correct Solution: ``` n,m,k = [int(a) for a in input().split() ] table = [] for i in range(n): table.append([int(a) for a in input().split() ]) # print(table) event = [] for i in range(k): event.append([int(a) for a in input().split() ]) multiplier = [0]*m subtract = [0]*n sumRes = [0]*n for i in range(k): sumRes[event[i][0]-1] -= 1 multiplier[event[i][1]-1] += 1 for i in range(n): for j in range(m): sumRes[i] = sumRes[i] + multiplier[j] * table[i][j] for x in sumRes: print(x,end=" ") ```
output
1
39,940
14
79,881
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,941
14
79,882
Tags: implementation Correct Solution: ``` from sys import stdin,stdout n,m,k=map(int,input().split()) l=[[0]*m]*n for i in range(n): l[i]=list(map(int,stdin.readline().split())) t=[[0]*2]*k e=[0]*n c=[0]*m for i in range(k): t0,t1=map(int,stdin.readline().split()) e[t0-1]-=1 c[t1-1]+=1 p=[""]*n for i in range(n): for j in range(m): e[i]=e[i]+c[j]*l[i][j] p[i]=str(e[i]) stdout.write(" ".join(p)) ```
output
1
39,941
14
79,883
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,942
14
79,884
Tags: implementation Correct Solution: ``` # -*- coding: utf-8 -*- """ YL 2 B. K6nelogi """ n,m,k=list(map(int,input().split())) too=dict() chat=dict() chat2=dict() for i in range(n): too[i]=0 for i in range(m): chat2[i]=0 # Korda postitati for i in range(n): chat[i]=list(map(int,input().split()))# info inimese kohta for i in range(k): x,y=list(map(int,input().split())) chat2[y-1]+=1 too[x-1]-=1 out=[] for i in range(n): t2=0 for e in range(m): if chat[i][e]: t2+=chat2[e] t=too[i]+t2 out.append(str(t)) print(' '.join(out)) ```
output
1
39,942
14
79,885
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,943
14
79,886
Tags: implementation Correct Solution: ``` n, m, k = map(int, input().split()) p, s, t = [[] for y in range(m)], [0] * n, [0] * m for x in range(n): for y, c in enumerate(input()[:: 2]): if c == '1': p[y].append(x) for i in range(k): x, y = map(int, input().split()) s[x - 1] -= 1 t[y - 1] += 1 for y, d in enumerate(t): for x in p[y]: s[x] += d print(' '.join(map(str, s))) ```
output
1
39,943
14
79,887
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,944
14
79,888
Tags: implementation Correct Solution: ``` R = lambda:map(int, input().split()) n, m, k = R() a = [list(R()) for _ in range(n)] b = [0] * n c = [0] * m for i in range(k): x, y = R() b[x - 1] += 1 c[y - 1] += 1 print(" ".join(map(str, (sum(a[i][j] * c[j] for j in range(m)) - b[i] for i in range(n))))) ```
output
1
39,944
14
79,889
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,945
14
79,890
Tags: implementation Correct Solution: ``` R = lambda:map(int, input().split()) n, m, k = R() a = [list(R()) for _ in range(n)] b = [0] * n c = [0] * m for i in range(k): x, y = R() b[x - 1] += 1 c[y - 1] += 1 print(" ".join(map(str, (sum(a[i][j] * c[j] for j in range(m)) - b[i] for i in range(n))))) # Made By Mostafa_Khaled ```
output
1
39,945
14
79,891
Provide tags and a correct Python 3 solution for this coding contest problem. The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 2·104; 1 ≤ m ≤ 10; 1 ≤ k ≤ 2·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n × m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m. Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 ≤ xi ≤ n; 1 ≤ yi ≤ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees. Output Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Output 3 3 1 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 Output 0 2 3 0
instruction
0
39,946
14
79,892
Tags: implementation Correct Solution: ``` R = lambda: map(int, input().split()) n, m, k = R() chat = [] for i in range(n): chat.append(list(R())) man = [0] * n; room = [0] * m for i in range(k): a, b = R() man[a-1] += 1 room[b-1] += 1 for i in range(n): t = 0 for j in range(m): if chat[i][j] == 1: t += room[j] print(t - man[i], end = ' ') ```
output
1
39,946
14
79,893