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. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets.
instruction
0
17,133
14
34,266
Tags: implementation Correct Solution: ``` c = 1 for i in range(int(input())): if i == 0 : a = input() else: b = input() c = c + (1 if a != b else 0) a = b print(c) ```
output
1
17,133
14
34,267
Provide tags and a correct Python 3 solution for this coding contest problem. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets.
instruction
0
17,134
14
34,268
Tags: implementation Correct Solution: ``` numberMagnets = int(input()) groups = 1 if numberMagnets == 1: print(1) else: lastMag = str(input()) for _ in range(numberMagnets-1): currMag = str(input()) if currMag != lastMag: groups += 1 lastMag = currMag print(groups) ```
output
1
17,134
14
34,269
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets. Submitted Solution: ``` n = int(input()) li = [input() for i in range(n)] count = 1 for i in range(n-1): if(li[i]!=li[i+1]): count += 1 print(count) ```
instruction
0
17,137
14
34,274
Yes
output
1
17,137
14
34,275
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets. Submitted Solution: ``` n = int(input()) no_of_grps = 0 magnet = ["01","10"] magnet[0] = str(input()) for i in range(1,n): magnet[1] = str(input()) if magnet[0][1] == magnet[1][0]: no_of_grps += 1 magnet[0] = magnet[1] print(no_of_grps + 1) ```
instruction
0
17,138
14
34,276
Yes
output
1
17,138
14
34,277
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets. Submitted Solution: ``` from itertools import groupby n=int(input()) l=[] for i in range(n): a=input() l.append(a[0]) m=0 for i,j in groupby(l): k=len(list(j)) if(k>m): m=k print(m) ```
instruction
0
17,139
14
34,278
No
output
1
17,139
14
34,279
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets. Submitted Solution: ``` n = int(input()) row = "" for i in range(n): row += input() groups = 1 for i in range(0, len(row)): try: if row[i] == row[i+1]: groups += 1 print(row[i], row[i+1], groups) except: pass print(groups) ```
instruction
0
17,141
14
34,282
No
output
1
17,141
14
34,283
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a "plus") and negative (a "minus"). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other. Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. <image> Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed. Input The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters "01", if Mike put the i-th magnet in the "plus-minus" position, or characters "10", if Mike put the magnet in the "minus-plus" position. Output On the single line of the output print the number of groups of magnets. Examples Input 6 10 10 10 01 10 10 Output 3 Input 4 01 01 10 10 Output 2 Note The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets. The second testcase has two groups, each consisting of two magnets. Submitted Solution: ``` T=int(input()) count=0 s="" for i in range(T): x=input() if s=="": s=s+x else: if s[-1] == x[0]: count = count else: count = count + 1 s = s + x if count==0: print(1) else: print(count) ```
instruction
0
17,142
14
34,284
No
output
1
17,142
14
34,285
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya. Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n. Let the queue consist of n - 1 people at the moment. Let's look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times. In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place. Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops. Input The first input line contains an integer n which is the number of people who has joined the queue (1 ≀ n ≀ 105). In the next n lines descriptions of the people are given in order of their coming β€” space-separated integers ai and ci (1 ≀ ai ≀ n, 0 ≀ ci ≀ n). Every description is located on s single line. All the ai's are different. Output Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space. Examples Input 2 1 0 2 1 Output 2 1 Input 3 1 3 2 3 3 3 Output 3 2 1 Input 5 2 3 1 4 4 3 3 1 5 2 Output 3 1 5 4 2 Submitted Solution: ``` n=int(input()) a=[] for i in range(n): q=input().split() a1=int(q[0]) a2=int(q[1]) a.append([a1,a2,i+1]) if i>0: z=i-a2 if a1<i+2: for j in range(i-1,i-a2-1,-1): if j<0: z=0 break elif a[j][0]>=a1: z=j+1 break ji=[a1,a2,i+1] for j in range(len(a)-1,z,-1): a[j]=a[j-1].copy() a[z]=ji.copy() s=str(a[0][2]) for i in range(1,len(a)): s+=' '+str(a[i][2]) print(s) ```
instruction
0
17,153
14
34,306
No
output
1
17,153
14
34,307
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya. Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n. Let the queue consist of n - 1 people at the moment. Let's look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times. In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place. Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops. Input The first input line contains an integer n which is the number of people who has joined the queue (1 ≀ n ≀ 105). In the next n lines descriptions of the people are given in order of their coming β€” space-separated integers ai and ci (1 ≀ ai ≀ n, 0 ≀ ci ≀ n). Every description is located on s single line. All the ai's are different. Output Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space. Examples Input 2 1 0 2 1 Output 2 1 Input 3 1 3 2 3 3 3 Output 3 2 1 Input 5 2 3 1 4 4 3 3 1 5 2 Output 3 1 5 4 2 Submitted Solution: ``` l=[] d={} for i in range(int(input())): a,c=map(int,input().split()) if i==0: l+=[1] d[1]=a else: cc=min(c,len(l)) #print(i+1,a,cc) j=1 for j in range(1,cc+1): #print(j) if d[l[-j]]>=a: j-=1 break l.insert(len(l)-j,i+1) d[i+1]=a #print(l) print(*l) ```
instruction
0
17,154
14
34,308
No
output
1
17,154
14
34,309
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya. Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n. Let the queue consist of n - 1 people at the moment. Let's look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times. In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place. Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops. Input The first input line contains an integer n which is the number of people who has joined the queue (1 ≀ n ≀ 105). In the next n lines descriptions of the people are given in order of their coming β€” space-separated integers ai and ci (1 ≀ ai ≀ n, 0 ≀ ci ≀ n). Every description is located on s single line. All the ai's are different. Output Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space. Examples Input 2 1 0 2 1 Output 2 1 Input 3 1 3 2 3 3 3 Output 3 2 1 Input 5 2 3 1 4 4 3 3 1 5 2 Output 3 1 5 4 2 Submitted Solution: ``` n=int(input()) a=[] for i in range(n): q=input().split() a1=int(q[0]) a2=int(q[1]) a.append([a1,a2,i+1]) if i>0: z=i-a2 if a1<i+2: for j in range(i-1,i-a2-1,-1): if j<0: z=0 break elif a[j][0]>=a1: z=j+1 break if z<0:z=0 ji=[a1,a2,i+1] for j in range(len(a)-1,z,-1): a[j]=a[j-1].copy() a[z]=ji.copy() s=str(a[0][2]) for i in range(1,len(a)): s+=' '+str(a[i][2]) print(s) ```
instruction
0
17,155
14
34,310
No
output
1
17,155
14
34,311
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya. Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n. Let the queue consist of n - 1 people at the moment. Let's look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times. In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place. Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops. Input The first input line contains an integer n which is the number of people who has joined the queue (1 ≀ n ≀ 105). In the next n lines descriptions of the people are given in order of their coming β€” space-separated integers ai and ci (1 ≀ ai ≀ n, 0 ≀ ci ≀ n). Every description is located on s single line. All the ai's are different. Output Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space. Examples Input 2 1 0 2 1 Output 2 1 Input 3 1 3 2 3 3 3 Output 3 2 1 Input 5 2 3 1 4 4 3 3 1 5 2 Output 3 1 5 4 2 Submitted Solution: ``` l=[] d={} for i in range(int(input())): a,c=map(int,input().split()) if i==0: l+=[1] d[1]=a else: cc=min(c,len(l)) for j in range(1,cc+1): #print(j) if d[l[-j]]>=a: j-=1 break l.insert(len(l)-j,i+1) d[i+1]=a #print(l) print(*l) ```
instruction
0
17,156
14
34,312
No
output
1
17,156
14
34,313
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,221
14
34,442
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = map(int, input().split()) a = sorted(list(map(int, input().split()))) print(min(min(a[n]/2, a[0])*3*n, w)) ```
output
1
17,221
14
34,443
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,222
14
34,444
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` friends_and_teapotsize = list(map(int, list(input().split()))) cup_size = list(map(int, list(input().split()))) total_boys = friends_and_teapotsize[0] total_teapot = friends_and_teapotsize[1] cup_size.sort(reverse=False) smallest_girl_cup = cup_size[0] smallest_boy_cup = cup_size[total_boys] tea_per_girl = 0 if smallest_girl_cup*2 < smallest_boy_cup: smallest_boy_cup = smallest_girl_cup*2 if smallest_girl_cup*2 > smallest_boy_cup: smallest_girl_cup = smallest_boy_cup/2 max_tea_per_girl = total_teapot/(3*total_boys) if max_tea_per_girl > smallest_girl_cup: max_tea_per_girl = smallest_girl_cup if max_tea_per_girl > smallest_boy_cup/2: max_tea_per_girl = smallest_boy_cup/2 result = max_tea_per_girl * total_boys * 3 print(result) ```
output
1
17,222
14
34,445
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,223
14
34,446
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = map(int, input().split(" ")) l = list(map(int, input().split(" "))) l = sorted(l) if l[0]*2 >= l[int(len(l)/2)]: if l[int(len(l)/2)] >= w/3*2/n: print(w) else: print(l[int(len(l)/2)]/2*n+l[int(len(l)/2)]*n) else: v = w/3 if l[0] >= v/n: print(w) else: print(l[0]*3*n) ```
output
1
17,223
14
34,447
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,224
14
34,448
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = map(int ,input().split()) read = list(map(int, input().split())) read.sort(reverse = True) c = min(read[-1], read[n - 1]/2) print(min(w, c * 3 * n)) ```
output
1
17,224
14
34,449
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,225
14
34,450
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = map(int, input().split()) t1 = w / (3 * n) a = list(map(int, input().split())) a.sort() a1 = a[n] a2 = a[0] if a1 / 2 <= a2: t = a1 / 2 else: t = a2 if t < t1: print(3 * n * t) else: print(w) ```
output
1
17,225
14
34,451
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,226
14
34,452
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = [int(i) for i in input().split()] capacity = [int(i) for i in input().split()] capacity.sort(reverse=True) result = min(capacity[(2*n) - 1] * 3 * n, capacity[n-1] * 3 / 2 * n, w) print(result) ```
output
1
17,226
14
34,453
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,227
14
34,454
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = (int(i) for i in input().split()) tea = sorted(int(i) for i in input().split()) smallest = min(tea[0], tea[n]/2) print(min(smallest*3*n, w)) ```
output
1
17,227
14
34,455
Provide tags and a correct Python 3 solution for this coding contest problem. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task...
instruction
0
17,228
14
34,456
Tags: constructive algorithms, implementation, math, sortings Correct Solution: ``` n, w = map(int, input().split()) a = sorted(list(map(int, input().split()))) print(min(3 * n * a[0], 1.5 * n * a[n], w)) ```
output
1
17,228
14
34,457
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task... Submitted Solution: ``` def answer(n,w,A): A.sort(reverse=True) maxb=A[n-1] maxg=A[-1] if maxb<=2*maxg: maxg=maxb/2 return min(3*n*maxg,w) n,w=map(int,input().split()) arr=list(map(int,input().split())) print(answer(n,w,arr)) ```
instruction
0
17,231
14
34,462
Yes
output
1
17,231
14
34,463
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task... Submitted Solution: ``` n, w = [int(i) for i in input().split()] lst = sorted([int(j) for j in input().split()]) h = min(lst[0], lst[n] / 2, w / (3 * n)) print(3 * h * n) ```
instruction
0
17,232
14
34,464
Yes
output
1
17,232
14
34,465
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of w milliliters and 2n tea cups, each cup is for one of Pasha's friends. The i-th cup can hold at most ai milliliters of water. It turned out that among Pasha's friends there are exactly n boys and exactly n girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: * Pasha can boil the teapot exactly once by pouring there at most w milliliters of water; * Pasha pours the same amount of water to each girl; * Pasha pours the same amount of water to each boy; * if each girl gets x milliliters of water, then each boy gets 2x milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, n and w (1 ≀ n ≀ 105, 1 ≀ w ≀ 109) β€” the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers ai (1 ≀ ai ≀ 109, 1 ≀ i ≀ 2n) β€” the capacities of Pasha's tea cups in milliliters. Output Print a single real number β€” the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. Examples Input 2 4 1 1 1 1 Output 3 Input 3 18 4 4 4 2 2 2 Output 18 Input 1 5 2 3 Output 4.5 Note Pasha also has candies that he is going to give to girls but that is another task... Submitted Solution: ``` from sys import stdin n, w = map(int, stdin.readline().split()) an = list(map(int, stdin.readline().split())) an.sort() g = an[0] b = an[n] t = w / 3.0 if g >= t / n: print(min(g, t / n) * n * 3) exit() if b >= (t * 2) / n: print(min(b, t * 2 / n) * n * 3 / 2) ```
instruction
0
17,234
14
34,468
No
output
1
17,234
14
34,469
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,298
14
34,596
Tags: dfs and similar, dp, dsu Correct Solution: ``` from collections import defaultdict class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members n,m,w=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) uf=UnionFind(n) for _ in range(m): x,y=map(int,input().split()) uf.union(x-1,y-1) l=uf.roots() ll=len(l) dp=[[-10**18]*(w+1) for i in range(ll)] ca,cb=0,0 dp[0][0]=0 for x in uf.members(l[0]): if a[x]<=w: dp[0][a[x]]=max(dp[0][a[x]],b[x]) ca+=a[x] cb+=b[x] if ca<=w: dp[0][ca]=max(dp[0][ca],cb) for i in range(1,ll): ca,cb=0,0 for x in uf.members(l[i]): for j in range(w+1): dp[i][j]=max(dp[i][j],dp[i-1][j]) if j-a[x]>=0: dp[i][j]=max(dp[i][j],dp[i-1][j-a[x]]+b[x]) ca+=a[x] cb+=b[x] for j in range(w+1): if j-ca>=0: dp[i][j]=max(dp[i][j],dp[i-1][j],dp[i-1][j-ca]+cb) ans=0 for j in range(w+1): ans=max(ans,dp[ll-1][j]) print(ans) ```
output
1
17,298
14
34,597
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,299
14
34,598
Tags: dfs and similar, dp, dsu Correct Solution: ``` f = lambda: map(int, input().split()) n, m, w = f() wb = [(0, 0)] + list(zip(f(), f())) t = list(range(n + 1)) def g(x): if x == t[x]: return x t[x] = g(t[x]) return t[x] for i in range(m): x, y = f() x, y = g(x), g(y) if x != y: t[y] = x p = [[] for j in range(n + 1)] for i in range(1, n + 1): p[g(i)].append(i) d = [1] + [0] * w for q in p: if len(q) > 1: WB = [wb[i] for i in q] SW = sum(q[0] for q in WB) SB = sum(q[1] for q in WB) for D in range(w, -1, -1): if d[D]: if D + SW <= w: d[D + SW] = max(d[D + SW], d[D] + SB) for W, B in WB: if D + W <= w: d[D + W] = max(d[D + W], d[D] + B) elif len(q) == 1: W, B = wb[q[0]] for D in range(w - W, -1, -1): if d[D]: d[D + W] = max(d[D + W], d[D] + B) print(max(d) - 1) ```
output
1
17,299
14
34,599
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,300
14
34,600
Tags: dfs and similar, dp, dsu Correct Solution: ``` R = lambda: map(int, input().split()) n, m, w = R() ws = list(R()) bs = list(R()) anc = [-1] * n def get(x): if anc[x] < 0: return x anc[x] = get(anc[x]) return anc[x] def join(x1, x2): x1, x2 = get(x1), get(x2) if x1 != x2: anc[x1] = x2 for i in range(m): x1, x2 = R() join(x1 - 1, x2 - 1) gs = [list() for i in range(n)] for i in range(n): gs[get(i)].append(i) gs = [x for x in gs if x] dp = [[0] * (w + 1) for i in range(len(gs) + 1)] for i in range(len(gs)): tw = sum(ws[k] for k in gs[i]) tb = sum(bs[k] for k in gs[i]) for j in range(w + 1): dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - tw] + tb) if j >= tw else dp[i - 1][j] for k in gs[i]: dp[i][j] = max(dp[i][j], (dp[i - 1][j - ws[k]] + bs[k] if j >= ws[k] else 0)) print(dp[len(gs) - 1][w]) ```
output
1
17,300
14
34,601
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,301
14
34,602
Tags: dfs and similar, dp, dsu Correct Solution: ``` def ler(): return [int(x) for x in input().split()] def dfs(u, adj, visited, s, Pesos, Belezas): visited[u] = True total_p = Pesos[u] total_b = Belezas[u] s.append(u) for v in adj[u]: if not visited[v]: w, b = dfs(v, adj, visited, s, Pesos, Belezas) total_p += w total_b += b return total_p, total_b n, m, w = ler() Pesos = ler() Belezas = ler() adj = [[] for _ in range(n)] for _ in range(m): x, y = ler() x -= 1 y -= 1 adj[x].append(y) adj[y].append(x) visited = [False] * n f = [0] * (w + 1) for i in range(n): if visited[i]: continue s = [] total_p, total_b = dfs(i, adj, visited, s, Pesos, Belezas) for j in range(w, -1, -1): jw = j + total_p if jw <= w: f[jw] = max(f[jw], f[j] + total_b) for v in s: jw = j + Pesos[v] if jw <= w: f[jw] = max(f[jw], f[j] + Belezas[v]) print(f[w]) ```
output
1
17,301
14
34,603
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,302
14
34,604
Tags: dfs and similar, dp, dsu Correct Solution: ``` f = lambda: map(int, input().split()) n, m, w = f() wb = [(0, 0)] + list(zip(f(), f())) t = list(range(n + 1)) def g(x): if x == t[x]: return x t[x] = g(t[x]) return t[x] for i in range(m): x, y = f() x, y = g(x), g(y) if x != y: t[y] = x p = [[] for j in range(n + 1)] for i in range(1, n + 1): p[g(i)].append(i) d = [1] + [0] * w for q in p: if len(q) > 1: WB = [wb[i] for i in q] SW = sum(q[0] for q in WB) SB = sum(q[1] for q in WB) for D in range(w, -1, -1): if d[D]: if D + SW <= w: d[D + SW] = max(d[D + SW], d[D] + SB) for W, B in WB: if D + W <= w: d[D + W] = max(d[D + W], d[D] + B) elif len(q) == 1: W, B = wb[q[0]] for D in range(w - W, -1, -1): if d[D]: d[D + W] = max(d[D + W], d[D] + B) print(max(d) - 1) # Made By Mostafa_Khaled ```
output
1
17,302
14
34,605
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,303
14
34,606
Tags: dfs and similar, dp, dsu Correct Solution: ``` hoses, pairs, total_weight = map(int, input().split()) weight_arr = list(map(int, input().split())) beauty_arr = list(map(int, input().split())) arr = [-1] * hoses def get(hose): if arr[hose] < 0: return hose arr[hose] = get(arr[hose]) return arr[hose] def join(left, right): left, right = get(left), get(right) if left != right: arr[left] = right for i in range(pairs): left, right = map(int, input().split()) join(left - 1, right - 1) groups = [list() for i in range(hoses)] for i in range(hoses): groups[get(i)].append(i) groups = [group for group in groups if group] dp = [[0] * (total_weight + 1) for i in range(len(groups) + 1)] for i in range(len(groups)): weight_sum = sum(weight_arr[x] for x in groups[i]) beauty_sum = sum(beauty_arr[x] for x in groups[i]) for j in range(total_weight + 1): dp[i][j] = max(beauty_sum + dp[i - 1][j - weight_sum] if weight_sum <= j else 0, dp[i - 1][j]) for k in groups[i]: dp[i][j] = max(dp[i][j], (dp[i - 1][j - weight_arr[k]] + beauty_arr[k] if weight_arr[k] <= j else 0)) print(dp[len(groups) - 1][total_weight]) ```
output
1
17,303
14
34,607
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,304
14
34,608
Tags: dfs and similar, dp, dsu Correct Solution: ``` from collections import defaultdict def read(): return list(map(int, input().split(' '))) def DSU(count): vs = list(range(count)) ss = [1] * count def get(i): if vs[i] == i: return i else: vs[i] = get(vs[i]) return vs[i] def unite(a, b): a = get(a) b = get(b) if a == b: return if ss[a] > ss[b]: vs[b] = a ss[a] += ss[b] else: vs[a] = b ss[b] += ss[a] def sets(): ds = defaultdict(list) for i in range(count): ds[get(i)] += [i] return ds res = lambda: None res.get = get res.unite = unite res.sets = sets return res n, m, w = read() ws = read() bs = read() dsu = DSU(n) for i in range(m): x, y = read() dsu.unite(x-1, y-1) sets = list(dsu.sets().values()) def calcSum(arr): return [sum([arr[i] for i in s]) for s in sets] sws = calcSum(ws) sbs = calcSum(bs) ans = [[0]*(w+1) for i in range(len(sets)+1)] for sn in range(1, len(sets) + 1): prevRow = ans[sn - 1] curSet = sets[sn - 1] for uw in range(1, w + 1): curAns = prevRow[uw] for item in curSet: if ws[item] <= uw: curAns = max(curAns, prevRow[uw - ws[item]] + bs[item]) if sws[sn-1] <= uw: curAns = max(curAns, prevRow[uw - sws[sn-1]] + sbs[sn-1]) ans[sn][uw] = curAns print(ans[len(sets)][w]) ```
output
1
17,304
14
34,609
Provide tags and a correct Python 3 solution for this coding contest problem. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
instruction
0
17,305
14
34,610
Tags: dfs and similar, dp, dsu Correct Solution: ``` from sys import stdin, stdout from collections import defaultdict as dd read, write = stdin.readline, stdout.write class DisjointSetUnion: def __init__(self, n): self.parent = list(range(n)) self.size = [1] * n self.num_sets = n def find(self, a): acopy = a while a != self.parent[a]: a = self.parent[a] while acopy != a: self.parent[acopy], acopy = a, self.parent[acopy] return a def union(self, a, b): a, b = self.find(a), self.find(b) if a != b: if self.size[a] < self.size[b]: a, b = b, a self.num_sets -= 1 self.parent[b] = a self.size[a] += self.size[b] def set_size(self, a): return self.size[self.find(a)] def __len__(self): return self.num_sets n, m, w = map(int, read().split()) weights = list(map(int, read().split())) beauties = list(map(int, read().split())) DSU = DisjointSetUnion(n) for _ in range(m): h1, h2 = map(int, read().split()) DSU.union(h1-1, h2-1) groups = dd(list) for i in range(n): DSU.find(i) for hose, parent in enumerate(DSU.parent): groups[parent].append(hose) dp = [0]*(w+1) for friends in groups.values(): dp_aux = dp[:] group_weight = group_beauty = 0 for friend in friends: f_weight, f_beauty = weights[friend], beauties[friend] group_weight += f_weight; group_beauty += f_beauty for weight in range(f_weight, w+1): dp[weight] = max(dp[weight], dp_aux[weight - f_weight] + f_beauty) for weight in range(group_weight, w+1): dp[weight] = max(dp[weight], dp_aux[weight - group_weight] + group_beauty) print(dp[-1]) ```
output
1
17,305
14
34,611
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` f = lambda: map(int, input().split()) n, m, s = f() wb = [(0, 0)] + list(zip(f(), f())) t = list(range(n + 1)) def g(x): if x == t[x]: return x t[x] = g(t[x]) return t[x] for i in range(m): x, y = f() x, y = g(x), g(y) if x != y: t[y] = x p = [[] for j in range(n + 1)] for i in range(1, n + 1): p[g(i)].append(i) d = [1] + [0] * s for q in p: if len(q) > 1: t = [wb[i] for i in q] t.append((sum(x[0] for x in t), sum(x[1] for x in t))) t.sort(key=lambda x: x[0]) for j in range(s, -1, -1): if d[j]: for w, b in t: if j + w > s: break d[j + w] = max(d[j + w], d[j] + b) elif len(q) == 1: w, b = wb[q[0]] for j in range(s - w, -1, -1): if d[j]: d[j + w] = max(d[j + w], d[j] + b) print(max(d) - 1) ```
instruction
0
17,306
14
34,612
Yes
output
1
17,306
14
34,613
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` def dfs(u, weights, beauties, adj, remaining, remainingg): w = weights[u] b = beauties[u] mw = [w] mb = [b] if u in adj: for v in adj[u]: if not remainingg[v]: continue remaining.remove(v) remainingg[v] = False pair = dfs(v, weights, beauties, adj, remaining, remainingg) w += pair[0] b += pair[1] mw += pair[2] mb += pair[3] return w, b, mw, mb n, m, w = map(int, input().split()) weights = list(map(int, input().split())) beauties = list(map(int, input().split())) adj = {} remaining = set([i for i in range(n)]) remainingg = n * [True] for _ in range(m): a, b = map(int, input().split()) a -= 1 b -= 1 if a not in adj: adj[a] = [] if b not in adj: adj[b] = [] adj[a].append(b) adj[b].append(a) ws = [] bs = [] mws = [] mbs = [] while len(remaining) > 0: xx = remaining.pop() remainingg[xx] = False pair = dfs(xx, weights, beauties, adj, remaining, remainingg) ws.append(pair[0]) bs.append(pair[1]) mws.append(pair[2]) mbs.append(pair[3]) # print(mws) tmws = [] tmbs = [] for i in range(len(mws)): tuples = [] for j in range(len(mws[i])): tuples.append((mws[i][j], mbs[i][j])) tuples.sort(key=lambda t: t[0]) tmws.append([]) tmbs.append([]) for t in tuples: tmws[-1].append(t[0]) tmbs[-1].append(t[1]) mws = tmws mbs = tmbs # print(ws) # print(bs) # print(mws) # print(mbs) n = len(ws) dp = [(w + 1) * [0] for _ in range(n)] i = 0 for j in range(w+1): if ws[i] <= j: dp[i][j] = bs[i] for k in range(len(mws[i])): ww = mws[i][k] bb = mbs[i][k] if ww <= j and bb > dp[i][j]: dp[i][j] = bb # print(dp) for i in range(1, n): for j in range(1, w + 1): if ws[i] <= j: dp[i][j] = max(bs[i] + dp[i - 1][j - ws[i]], dp[i - 1][j]) else: dp[i][j] = dp[i - 1][j] for k in range(len(mws[i])): ww = mws[i][k] bb = mbs[i][k] if ww <= j: dp[i][j] = max(bb + dp[i - 1][j - ww], dp[i - 1][j], dp[i][j]) else: break # print(dp) print(dp[n - 1][w]) ```
instruction
0
17,307
14
34,614
Yes
output
1
17,307
14
34,615
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` def inp(): return [int(x) for x in input().split()] def dfs(u, adj, visited, s, W, B): visited[u] = True total_w = W[u] total_b = B[u] s.append(u) for v in adj[u]: if not visited[v]: w, b = dfs(v, adj, visited, s, W, B) total_w += w total_b += b return total_w, total_b def main(): n, m, w = inp() W = inp() B = inp() adj = [[] for _ in range(n)] for _ in range(m): x, y = inp() x -= 1 y -= 1 adj[x].append(y) adj[y].append(x) visited = [False] * n f = [0] * (w + 1) for i in range(n): if visited[i]: continue s = [] total_w, total_b = dfs(i, adj, visited, s, W, B) for j in range(w, -1, -1): jw = j + total_w if jw <= w: f[jw] = max(f[jw], f[j] + total_b) for v in s: jw = j + W[v] if jw <= w: f[jw] = max(f[jw], f[j] + B[v]) print(f[w]) if __name__ == "__main__": main() ```
instruction
0
17,308
14
34,616
Yes
output
1
17,308
14
34,617
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` R = lambda: map(int, input().split()) n, m, w = R() ws = list(R()) bs = list(R()) anc = [-1] * n def get(x): if anc[x] < 0: return x anc[x] = get(anc[x]) return anc[x] def join(x1, x2): x1, x2 = get(x1), get(x2) if x1 != x2: anc[x1] = x2 for i in range(m): x1, x2 = R() join(x1 - 1, x2 - 1) gs = [list() for i in range(n)] for i in range(n): gs[get(i)].append(i) gs = [x for x in gs if x] dp = [[0] * (w + 1) for i in range(len(gs) + 1)] for i in range(len(gs)): tw = sum(ws[k] for k in gs[i]) tb = sum(bs[k] for k in gs[i]) for j in range(w + 1): dp[i][j] = max(dp[i][j], dp[i - 1][j], (dp[i - 1][j - tw] + tb if j >= tw else 0)) for k in gs[i]: dp[i][j] = max(dp[i][j], dp[i - 1][j], (dp[i - 1][j - ws[k]] + bs[k] if j >= ws[k] else 0)) print(dp[len(gs) - 1][w]) ```
instruction
0
17,309
14
34,618
Yes
output
1
17,309
14
34,619
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` n, m, mw = map(int, input().split()) mw += 1 we = list(map(int, input().split())) ce = list(map(int, input().split())) graph = [[] for i in range(n)] for i in range(m): u, v = map(int, input().split()) graph[u - 1].append(v -1) graph[v-1].append(u-1) used = [False] * n w = [] c = [] for i in range(n): if not used[i]: stack = [i] res = [] resc = [] while stack: cur = stack.pop() used[cur] = True res.append(we[cur]) resc.append(ce[cur]) for u in graph[cur]: if not used[u]: stack.append(u) res.append(sum(res)) resc.append(sum(resc)) w.append(res) c.append(resc) m = 0 F = [[0] * mw for i in range(len(w))] for i in range(len(w)): for k in range(mw): for g in range(len(w[i])): if k >= w[i][g]: F[i][k] = max(F[i - 1][k], F[i - 1][k - w[i][g]] + c[i][g], F[i][k]) else: F[i][k] = max(F[i][k], F[i-1][k-1]) print(F[-1][-1]) ```
instruction
0
17,310
14
34,620
No
output
1
17,310
14
34,621
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ####################################### import sys,threading sys.setrecursionlimit(600000) threading.stack_size(10**8) def dfs(x): global a,b,v,adj,l1,l2,w,l3 v[x]=1 a+=l1[x-1] b+=l2[x-1] if l1[x-1]<=w: l3.append([l1[x-1],l2[x-1]]) for i in adj[x]: if not v[i]: dfs(i) def main(): global a,b,v,adj,l1,l2,w,l3 n,m,w=map(int,input().split()) l1=list(map(int,input().split())) l2=list(map(int,input().split())) v=[0]*(n+1) adj=[[] for i in range(n+1)] for i in range(m): x,y=map(int,input().split()) adj[x].append(y) adj[y].append(x) l=[] dp=[0]*1001 for i in range(1,n+1): if not v[i]: a=0 b=0 l3=[] dfs(i) if a<=w: l3.append([a,b]) l.append(l3) for i in l: dp1=dp.copy() for j in i: dp[j[0]]=max(dp1[j[0]],j[1]) for j in i: for k in range(j[0]+1,w+1): if dp1[k-j[0]]: dp[k]=max(dp1[k],dp1[k-j[0]]+j[1]) print(max(dp[:w+1])) t=threading.Thread(target=main) t.start() t.join() ```
instruction
0
17,311
14
34,622
No
output
1
17,311
14
34,623
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` from collections import defaultdict def read(): return list(map(int, input().split(' '))) def DSU(count): vs = list(range(count)) ss = [1] * count def get(i): if vs[i] == i: return i else: vs[i] = get(vs[i]) return vs[i] def unite(a, b): if ss[a] > ss[b]: vs[b] = a ss[a] += ss[b] else: vs[a] = b ss[b] += ss[a] def sets(): ds = defaultdict(list) for i in range(count): ds[get(i)] += [i] return ds res = lambda: None res.get = get res.unite = unite res.sets = sets return res n, m, w = read() ws = read() bs = read() dsu = DSU(n) for i in range(m): x, y = read() dsu.unite(x-1, y-1) sets = list(dsu.sets().values()) def calcSum(arr): return [sum([arr[i] for i in s]) for s in sets] sws = calcSum(ws) sbs = calcSum(bs) ans = [[0]*(w+1) for i in range(len(sets)+1)] for sn in range(1, len(sets) + 1): for uw in range(1, w + 1): curAns = ans[sn - 1][uw] for item in sets[sn-1]: if ws[item] <= uw: curAns = max(curAns, ans[sn - 1][uw - ws[item]] + bs[item]) if sws[sn-1] <= uw: curAns = max(curAns, ans[sn - 1][uw - sws[sn-1]] + sbs[sn-1]) ans[sn][uw] = curAns print(ans[len(sets)][w]) ```
instruction
0
17,312
14
34,624
No
output
1
17,312
14
34,625
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 ≀ i < k, and a1 = x and ak = y. <image> Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it. Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w. Input The first line contains integers n, m and w (1 ≀ n ≀ 1000, <image>, 1 ≀ w ≀ 1000) β€” the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited. The second line contains n integers w1, w2, ..., wn (1 ≀ wi ≀ 1000) β€” the weights of the Hoses. The third line contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 106) β€” the beauties of the Hoses. The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≀ xi, yi ≀ n, xi β‰  yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct. Output Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w. Examples Input 3 1 5 3 2 5 2 4 2 1 2 Output 6 Input 4 2 11 2 4 6 6 6 4 2 1 1 2 2 3 Output 7 Note In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6. In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7. Submitted Solution: ``` from collections import defaultdict def dfs(v, visited, group): visited.add(v) group.add(v) for neighbour in graph[v]: if neighbour not in visited: dfs(neighbour, visited, group) n, m, weight = map(int, input().split()) ps = list(map(int, input().split())) vs = list(map(int, input().split())) graph = defaultdict(list) for i in range(m): h1, h2 = map(int, input().split()) graph[h1-1].append(h2-1) graph[h2-1].append(h1-1) # pegar grupos groups = [] visited = set() for vertex in list(graph): if vertex not in visited: groups.append(set()) dfs(vertex, visited, groups[len(groups)-1]) # adicionar cavalos solitΓ‘rios for i in range(n): if i not in visited: groups.append(set([i])) print(groups) #------------------------------------------------------ master = [0] * (weight + 1) index = 0 for group in groups: matrix = [master] for i in range(1, len(group) + 1): matrix.append([0] * (weight + 1)) pgrupo = 0 vgrupo = 0 for i in range(1, len(group) + 1): pi = ps[index + i -1] vi = vs[index + i -1] pgrupo += pi vgrupo += vi for p in range(1, weight + 1): com = 0 if pi <= p: com = matrix[0][max(p-pi, 0)] + vi sem = matrix[i-1][p] matrix[i][p] = max(com, sem) new_master = [0] * (weight + 1) for p in range(1, weight + 1): com = 0 if pgrupo <= p: com = matrix[0][max(p-pgrupo, 0)] + vgrupo sem = matrix[len(matrix)-1][p] new_master[p] = max(com, sem) master = new_master index += len(group) print(matrix) print(new_master[-1]) ```
instruction
0
17,313
14
34,626
No
output
1
17,313
14
34,627
Provide tags and a correct Python 3 solution for this coding contest problem. There is an automatic door at the entrance of a factory. The door works in the following way: * when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, * when one or several people come to the door and it is open, all people immediately come inside, * opened door immediately closes in d seconds after its opening, * if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if d = 3 and four people are coming at four different moments of time t1 = 4, t2 = 7, t3 = 9 and t4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that n employees will enter at moments a, 2Β·a, 3Β·a, ..., nΒ·a (the value a is positive integer). Also m clients will enter at moments t1, t2, ..., tm. Write program to find the number of times the automatic door will open. Assume that the door is initially closed. Input The first line contains four integers n, m, a and d (1 ≀ n, a ≀ 109, 1 ≀ m ≀ 105, 1 ≀ d ≀ 1018) β€” the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence t1, t2, ..., tm (1 ≀ ti ≀ 1018) β€” moments of time when clients will come. The values ti are given in non-decreasing order. Output Print the number of times the door will open. Examples Input 1 1 3 4 7 Output 1 Input 4 3 4 2 7 9 11 Output 4 Note In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
instruction
0
17,368
14
34,736
Tags: implementation Correct Solution: ``` BigNum = 10 ** 20 n, m, a, d = map(int, input().split(' ')) ts = [0] + list(map(int, input().split(' '))) + [BigNum] def empsInRange(l, r): em1 = l // a + 1 em2 = r // a return (em1, min(em2, n)) empDoorGroup = d // a + 1 def moveEmps(emps, last): em1, em2 = emps if em1 > em2: return last, 0 if em1 * a <= last + d: gr1 = (last + d - em1 * a) // a em1 += 1 + gr1 if em1 > em2: return last, 0 doorGroups = (em2 - em1 + 1 + empDoorGroup - 1) // empDoorGroup last = (em1 + empDoorGroup * (doorGroups - 1)) * a return last, doorGroups res = 0 last = -BigNum for i in range(1, len(ts)): #print(i, ' ------------ ') emps = empsInRange(ts[i - 1], ts[i]) #print(ts[i-1], ts[i], emps, last) last, inc = moveEmps(emps, last) #print('last:', last, ' inc:', inc) res += inc if ts[i] < BigNum and last + d < ts[i]: res += 1 last = ts[i] #print('temp res:', res) print(res) ```
output
1
17,368
14
34,737
Provide tags and a correct Python 3 solution for this coding contest problem. There is an automatic door at the entrance of a factory. The door works in the following way: * when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, * when one or several people come to the door and it is open, all people immediately come inside, * opened door immediately closes in d seconds after its opening, * if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if d = 3 and four people are coming at four different moments of time t1 = 4, t2 = 7, t3 = 9 and t4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that n employees will enter at moments a, 2Β·a, 3Β·a, ..., nΒ·a (the value a is positive integer). Also m clients will enter at moments t1, t2, ..., tm. Write program to find the number of times the automatic door will open. Assume that the door is initially closed. Input The first line contains four integers n, m, a and d (1 ≀ n, a ≀ 109, 1 ≀ m ≀ 105, 1 ≀ d ≀ 1018) β€” the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence t1, t2, ..., tm (1 ≀ ti ≀ 1018) β€” moments of time when clients will come. The values ti are given in non-decreasing order. Output Print the number of times the door will open. Examples Input 1 1 3 4 7 Output 1 Input 4 3 4 2 7 9 11 Output 4 Note In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
instruction
0
17,369
14
34,738
Tags: implementation Correct Solution: ``` def solve(): n1, m, a, d = list(map(int, input().split())) t = list(map(int, input().split())) from bisect import insort from math import floor insort(t, a * n1) pred = 0 k = 0 kpred = 0 n = 0 step = d // a + 1 sol = 0 fl = 0 for i in t: if (i > pred): if fl == 0: n = (i - pred + (pred % a)) // a if n != 0: k += (n // step) * step - step * (n % step == 0) + 1 if k > n1: k = n1 fl = 1 # print(k) if (k * a + d >= i) and (n != 0): pred = k * a + d else: pred = i + d k = floor(pred // a) sol += 1 # if n==0: k = min(floor(pred // a), n1) sol += n // step + (n % step != 0) else: sol += 1 pred = i + d if i == a * n1: fl = 1 # print(i,pred,sol,n,step,k, fl) print(sol) solve() ```
output
1
17,369
14
34,739
Provide tags and a correct Python 3 solution for this coding contest problem. There is an automatic door at the entrance of a factory. The door works in the following way: * when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, * when one or several people come to the door and it is open, all people immediately come inside, * opened door immediately closes in d seconds after its opening, * if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if d = 3 and four people are coming at four different moments of time t1 = 4, t2 = 7, t3 = 9 and t4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that n employees will enter at moments a, 2Β·a, 3Β·a, ..., nΒ·a (the value a is positive integer). Also m clients will enter at moments t1, t2, ..., tm. Write program to find the number of times the automatic door will open. Assume that the door is initially closed. Input The first line contains four integers n, m, a and d (1 ≀ n, a ≀ 109, 1 ≀ m ≀ 105, 1 ≀ d ≀ 1018) β€” the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence t1, t2, ..., tm (1 ≀ ti ≀ 1018) β€” moments of time when clients will come. The values ti are given in non-decreasing order. Output Print the number of times the door will open. Examples Input 1 1 3 4 7 Output 1 Input 4 3 4 2 7 9 11 Output 4 Note In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
instruction
0
17,370
14
34,740
Tags: implementation Correct Solution: ``` n1,m,a,d=list(map(int,input().split())) t=list(map(int,input().split())) from bisect import * from math import * insort(t,a*n1) pred=0 k=0 kpred=0 n=0 step=d//a+1 sol=0 fl=0 for i in t: if (i > pred): if fl == 0: n=(i-pred+(pred%a))//a if n!=0: k+=(n//step)*step - step*(n % step == 0) + 1 if k > n1: k=n1 fl=1 #print(k) if (k*a+d>=i) and (n!=0): pred=k*a+d else: pred=i+d k=floor(pred//a) sol+=1 #if n==0: k=min(floor(pred//a),n1) sol+=n//step+(n%step!=0) else: sol+=1 pred=i+d if i==a*n1: fl=1 #print(i,pred,sol,n,step,k, fl) print(sol) ```
output
1
17,370
14
34,741
Provide tags and a correct Python 3 solution for this coding contest problem. There is an automatic door at the entrance of a factory. The door works in the following way: * when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, * when one or several people come to the door and it is open, all people immediately come inside, * opened door immediately closes in d seconds after its opening, * if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if d = 3 and four people are coming at four different moments of time t1 = 4, t2 = 7, t3 = 9 and t4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that n employees will enter at moments a, 2Β·a, 3Β·a, ..., nΒ·a (the value a is positive integer). Also m clients will enter at moments t1, t2, ..., tm. Write program to find the number of times the automatic door will open. Assume that the door is initially closed. Input The first line contains four integers n, m, a and d (1 ≀ n, a ≀ 109, 1 ≀ m ≀ 105, 1 ≀ d ≀ 1018) β€” the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence t1, t2, ..., tm (1 ≀ ti ≀ 1018) β€” moments of time when clients will come. The values ti are given in non-decreasing order. Output Print the number of times the door will open. Examples Input 1 1 3 4 7 Output 1 Input 4 3 4 2 7 9 11 Output 4 Note In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
instruction
0
17,371
14
34,742
Tags: implementation Correct Solution: ``` def solve(): n1, m, a, d = list(map(int, input().split())) t = list(map(int, input().split())) from bisect import insort from math import floor insort(t, a * n1) pred = 0 k = 0 kpred = 0 n = 0 step = d // a + 1 sol = 0 fl = 0 for i in t: if (i > pred): if fl == 0: n = (i - pred + (pred % a)) // a if n != 0: k += (n // step) * step - step * (n % step == 0) + 1 if k > n1: k = n1 fl = 1 # print(k) if (k * a + d >= i) and (n != 0): pred = k * a + d else: pred = i + d k = floor(pred // a) sol += 1 # if n==0: k = min(floor(pred // a), n1) sol += n // step + (n % step != 0) else: sol += 1 pred = i + d if i == a * n1: fl = 1 # print(i,pred,sol,n,step,k, fl) print(sol) solve() # Made By Mostafa_Khaled ```
output
1
17,371
14
34,743
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is an automatic door at the entrance of a factory. The door works in the following way: * when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, * when one or several people come to the door and it is open, all people immediately come inside, * opened door immediately closes in d seconds after its opening, * if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if d = 3 and four people are coming at four different moments of time t1 = 4, t2 = 7, t3 = 9 and t4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that n employees will enter at moments a, 2Β·a, 3Β·a, ..., nΒ·a (the value a is positive integer). Also m clients will enter at moments t1, t2, ..., tm. Write program to find the number of times the automatic door will open. Assume that the door is initially closed. Input The first line contains four integers n, m, a and d (1 ≀ n, a ≀ 109, 1 ≀ m ≀ 105, 1 ≀ d ≀ 1018) β€” the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence t1, t2, ..., tm (1 ≀ ti ≀ 1018) β€” moments of time when clients will come. The values ti are given in non-decreasing order. Output Print the number of times the door will open. Examples Input 1 1 3 4 7 Output 1 Input 4 3 4 2 7 9 11 Output 4 Note In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time. Submitted Solution: ``` def solve(i,j,r,k,h): n,m,a,d=map(int,input().split()) c=[int(x) for x in input().split()] # ptr while i<m and j<n: if c[i]<(j+1)*a: if i==0 and h: k=c[i] r+=1 h=0 else: if c[i]-k<=d: pass else: r+=1 k=c[i] i+=1 else: if j==0 and h: k=(j+1)*a r+=1 h=0 else: if (j+1)*a-k<=d: pass else: r+=1 k=(j+1)*a j+=1 while i<m: if c[i]-k<=d: pass else: r+=1 k=c[i] i+=1 # there need a improvement while j<n: if (j+1)*a-k<=d: j+=1 continue else: if a>d: r+=n-j else: import math h=math.ceil(d/a) r+=(n-j)//h j=n print(solve(0,0,0,0,1)) ```
instruction
0
17,373
14
34,746
No
output
1
17,373
14
34,747
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,885
14
35,770
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` import io import os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline class DSU: def __init__(self, n): self.parent = list(range(n)) def add(self): n = len(self.parent) self.parent.append(n) return n def find(self, v): if v == self.parent[v]: return v self.parent[v] = self.find(self.parent[v]) return self.parent[v] def union(self, a, b): a = self.find(a) b = self.find(b) if a != b: parent[b] = a def solve(): n = int(input()) vals = [] dsu = DSU(n) par = [-1] * n g = [] all_vals = [] for i in range(n): ai = list(map(int, input().split())) vals.append(ai[i]) g.append(ai) for j in range(i+1, n): all_vals.append((ai[j], i, j)) all_vals.sort() for val, i, j in all_vals: if dsu.find(i) == dsu.find(j): continue if vals[dsu.find(i)] != val: new = dsu.add() vals.append(val) par.append(-1) par[dsu.find(i)] = new dsu.parent[dsu.find(i)] = new par[dsu.find(j)] = new dsu.parent[dsu.find(j)] = new print(len(vals)) print(*vals) print(len(vals)) for i in range(len(vals)-1): print(i+1, par[i]+1) t = 1 for _ in range(t): solve() ```
output
1
17,885
14
35,771
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,886
14
35,772
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` import io import os from collections import Counter, defaultdict, deque def solve(N, A): parent = [-1] * N children = [[]] * N costs = [A[i][i] for i in range(N)] leafs = list(range(N)) q = deque([(leafs, -1)]) while q: leafs, p = q.pop() assert len(leafs) >= 2 rootCost = 0 for i in range(len(leafs)): for j in range(i + 1, len(leafs)): rootCost = max(rootCost, A[leafs[i]][leafs[j]]) comps = [[leafs[0]]] for i in range(1, len(leafs)): x = leafs[i] for comp in comps: if A[comp[0]][x] != rootCost: comp.append(x) break else: comps.append([x]) assert len(comps) > 1 for c1 in comps[0]: for c2 in comps[1]: assert A[c1][c2] == rootCost newId = len(children) children.append([]) parent.append(p) costs.append(rootCost) if p != -1: children[p].append(newId) for comp in comps: if len(comp) == 1: parent[comp[0]] = newId children[newId].append(comp[0]) else: q.append((comp, newId)) root = N K = len(parent) return ( str(K) + "\n" + " ".join(str(c) for c in costs) + "\n" + str(root + 1) + "\n" + "\n".join( str(i + 1) + " " + str(parent[i] + 1) for i in range(K) if i != root ) ) if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline TC = 1 for tc in range(1, TC + 1): (N,) = [int(x) for x in input().split()] A = [[int(x) for x in input().split()] for i in range(N)] ans = solve(N, A) print(ans) ```
output
1
17,886
14
35,773
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,887
14
35,774
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` class node: def __init__(self,id_no,val): self.id_no = id_no self.val = val self.child = [] import sys,functools,collections,bisect,math,heapq input = sys.stdin.readline def dfs(root,pre): values.append((root.id_no,root.val,pre)) for child in root.child: dfs(child,root.id_no) def construct(a): global x if len(a) == 1: return node(a[0]+1,arr[a[0]][a[0]]) curr = 0 ch = collections.defaultdict(list) for i in range(len(a)): for j in range(i+1,len(a)): if arr[a[i]][a[j]] >= curr: curr = arr[a[i]][a[j]] for i in a: for j in range(len(ch)+1): for k in ch[j]: if arr[k][i] == curr: break else: ch[j].append(i) break root = node(x,curr) x += 1 for i in ch: root.child.append(construct(ch[i])) return root n = int(input()) x = n+1 a = list(range(n)) arr = [] for i in range(n): arr.append(list(map(int,input().strip().split()))) root = construct(a) values = [] dfs(root,-1) #print(values) N = len(values) v = [0]*N parent = [] for i in values: v[i[0]-1] = i[1] if i[2] != -1: parent.append(str(i[0])+' '+str(i[2])) print(len(v)) print(' '.join(str(i) for i in v)) print(root.id_no) print('\n'.join(parent)) ```
output
1
17,887
14
35,775
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,888
14
35,776
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` import bisect nodes = [] class Node: def __init__(self, idx, salary): self.idx = idx self.salary = salary self.children = [] nodes.append(self) def find_parent(board, parent_node, row, y): for child in parent_node.children: if board[child.idx][y] == parent_node.salary: continue else: return find_parent(board, child, row, y) return parent_node def main(): n = int(input()) board = [] for i in range(n): a = list(map(int, input().split())) board.append(a) tree = {} idx = 0 #board[idx].sort() row = sorted(board[idx]) prev = None for i in range(n): salary = row[i] if prev and prev.salary == salary: continue node = Node(idx, salary) if prev: node.children.append(prev) prev = node tree[salary] = node for i in range(1, n): intersect = board[i][0] row = sorted(board[i]) pos = bisect.bisect_left(row, intersect) prev = find_parent(board, tree[intersect], row, i) #print(prev.salary, "!") #for j in range(pos - 2, -1, -1): for j in range(pos - 1, -1, -1): if row[j] >= prev.salary: continue node = Node(i, row[j]) prev.children.append(node) prev = node print(len(nodes)) nodes.sort(key = lambda x : len(x.children)) Mx = 0 sals = [] for i in range(len(nodes)): sals.append(nodes[i].salary) if nodes[Mx].salary < nodes[i].salary: Mx = i nodes[i].index = i print(*sals) print(Mx + 1) to_do = [Mx]; z = 0 while z < len(to_do): current = to_do[z] z += 1 for child in nodes[current].children: print(child.index + 1, nodes[current].index + 1) to_do.append(child.index) main() ```
output
1
17,888
14
35,777
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,889
14
35,778
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) n=int(input()) uf=UnionFind(n*n) grid=[] ans=[] edges=[] for i in range(n): a=list(map(int,input().split())) grid.append(a) relations=[] for i in range(n): for j in range(i,n): if i==j: ans.append(grid[i][j]) continue relations.append((i,j,grid[i][j])) relations.sort(key=lambda thing: thing[2]) rootfinder=[0]*(500*500) for i in range(n): rootfinder[i]=i k=n for p in range(len(relations)): i,j,value=relations[p] if uf.same(i,j): continue a=uf.find(i) b=uf.find(j) l=rootfinder[a] m=rootfinder[b] if ans[l]==value: edges.append([m+1,l+1]) uf.union(a,b) newroot=uf.find(a) rootfinder[newroot]=l elif ans[m]==value: edges.append([l+1,m+1]) uf.union(a,b) newroot=uf.find(a) rootfinder[newroot]=m else: ans.append(value) uf.union(a,b) newroot=uf.find(a) rootfinder[newroot]=k edges.append([m+1,k+1]) edges.append([l+1,k+1]) k+=1 print(k) print(*ans) print(rootfinder[uf.find(0)]+1) for edge in edges: print(*edge) ```
output
1
17,889
14
35,779
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,890
14
35,780
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` n = int(input()) a = [list(map(int, input().split())) for i in range(n)] val = [-1]*(10**6) par = [-1]*(10**6) for i in range(n): val[i] = a[i][i] edgetank = [] for i in range(n): for j in range(i+1, n): edgetank.append((a[i][j], i, j)) edgetank.sort() def findroot(x): now = x while par[now] >= 0: now = par[now] return now newvertex = n for w, u, v in edgetank: u_root = findroot(u) v_root = findroot(v) if u_root == v_root: continue if val[u_root] == w: par[v_root] = u_root elif val[v_root] == w: par[u_root] = v_root else: par[u_root] = newvertex par[v_root] = newvertex val[newvertex] = w newvertex += 1 print(newvertex) print(*val[:newvertex]) emp_sup = [] for i in range(newvertex): if par[i] != -1: emp_sup.append('{} {}'.format(i+1, par[i]+1)) #emp_sup.sort() print(newvertex) print(*emp_sup, sep='\n') ```
output
1
17,890
14
35,781
Provide tags and a correct Python 3 solution for this coding contest problem. The Dogeforces company has k employees. Each employee, except for lower-level employees, has at least 2 subordinates. Lower-level employees have no subordinates. Each employee, except for the head of the company, has exactly one direct supervisor. The head of the company is a direct or indirect supervisor of all employees. It is known that in Dogeforces, each supervisor receives a salary strictly more than all his subordinates. The full structure of the company is a secret, but you know the number of lower-level employees and for each pair of lower-level employees, the salary of their common supervisor is known (if there are several such supervisors, then the supervisor with the minimum salary). You have to restore the structure of the company. Input The first line contains a single integer n (2 ≀ n ≀ 500) β€” the number of lower-level employees. This is followed by n lines, where i-th line contains n integers a_{i,1}, a_{i,2}, ..., a_{i,n} (1 ≀ a_{i,j} ≀ 5000) β€” salary of the common supervisor of employees with numbers i and j. It is guaranteed that a_{i,j} = a_{j,i}. Note that a_{i,i} is equal to the salary of the i-th employee. Output In the first line, print a single integer k β€” the number of employees in the company. In the second line, print k integers c_1, c_2, ..., c_k, where c_i is the salary of the employee with the number i. In the third line, print a single integer r β€” the number of the employee who is the head of the company. In the following k-1 lines, print two integers v and u (1 ≀ v, u ≀ k) β€” the number of the employee and his direct supervisor. Note that the lower-level employees have numbers from 1 to n, and for the rest of the employees, you have to assign numbers from n+1 to k. If there are several correct company structures, you can print any of them. Example Input 3 2 5 7 5 1 7 7 7 4 Output 5 2 1 4 7 5 4 1 5 2 5 5 4 3 4 Note One of the possible structures in the first example: <image>
instruction
0
17,891
14
35,782
Tags: constructive algorithms, data structures, dfs and similar, divide and conquer, dsu, greedy, sortings, trees Correct Solution: ``` import sys,math,itertools from collections import Counter,deque,defaultdict from bisect import bisect_left,bisect_right from heapq import heappop,heappush,heapify, nlargest from copy import deepcopy mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split())) def inps(): return sys.stdin.readline() def inpsl(x): tmp = sys.stdin.readline(); return list(tmp[:x]) def err(x): print(x); exit() class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} n = inp() a = [inpl() for _ in range(n)] lis = [] res = [] for i in range(n): for j in range(i,n): if i==j: res.append(a[i][j]) else: lis.append((a[i][j],i,j)) lis.sort() pa = [-1]*2000 mostpa = [-1]*n idx = n nowsal = lis[0][0] vers = [] uf = UnionFind(n) # def sol(nowsal,vers,idx): # res.append(nowsal) # for u in list(vers): # u->[0,n) # if mostpa[u] != -1: # pa[mostpa[u]] = idx # mostpa[u] = idx # if pa[u] == -1: # pa[u] = idx def sol(nowsal,vers): global idx for u,v in vers: uf.union(u,v) pat = defaultdict(list) for u,v in vers: x = uf.find(u) pat[x].append(u) pat[x].append(v) for u in pat.values(): for v in u: if pa[v] == -1: pa[v] = idx else: pa[mostpa[v]] = idx mostpa[v] = idx res.append(nowsal) idx += 1 for sal,i,j in lis: if sal != nowsal: sol(nowsal,vers) nowsal = sal; vers = [] vers.append((i,j)) sol(nowsal,vers) ln = len(res) print(ln) print(*res) print(ln) for i in range(ln-1): print(i+1,pa[i]+1) ```
output
1
17,891
14
35,783