message stringlengths 2 22.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 145 109k | cluster float64 9 9 | __index_level_0__ int64 290 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance. | instruction | 0 | 41,275 | 9 | 82,550 |
Tags: greedy
Correct Solution:
```
from sys import exit, setrecursionlimit
setrecursionlimit(10**6)
inn = lambda: input().strip()
mapp = lambda: map(int, inn().split(" "))
N = int(inn())
arr = list(enumerate(mapp()))
arr.sort(key=lambda x: x[1])
answer = 0
sasha = dima = 0
for i in range(0, len(arr), 2):
sasha_A, sasha_B = abs(sasha - arr[i][0]), abs(sasha - arr[i+1][0])
dima_A, dima_B = abs(dima - arr[i][0]), abs(dima - arr[i+1][0])
if sasha_A + dima_B < sasha_B + dima_A:
answer += sasha_A
answer += dima_B
sasha = arr[i][0]
dima = arr[i+1][0]
else:
answer += sasha_B
answer += dima_A
sasha = arr[i+1][0]
dima = arr[i][0]
print(answer)
``` | output | 1 | 41,275 | 9 | 82,551 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
n = int(input())
a = list(map(int, input().split()))
s = {}
for q in range(len(a)):
s[a[q]] = s.get(a[q], [])+[q]
ans = x = y = 0
for q in range(1, n+1):
if abs(x-s[q][0])+abs(y-s[q][1]) > abs(x-s[q][1])+abs(y-s[q][0]):
ans += abs(x-s[q][1])+abs(y-s[q][0])
else:
ans += abs(x-s[q][0])+abs(y-s[q][1])
x, y = s[q][0], s[q][1]
print(ans)
``` | instruction | 0 | 41,276 | 9 | 82,552 |
Yes | output | 1 | 41,276 | 9 | 82,553 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
from sys import stdin, stdout
import math
N = int(input())
#N,M,K = [int(x) for x in stdin.readline().split()]
arr = [int(x) for x in stdin.readline().split()]
order = sorted(range(len(arr)), key=lambda k: arr[k])
#print(order)
locate = [order[0],order[1]]
res = order[0] + order[1]
for i in range(1,N):
A = order[2*i]
B = order[2*i+1]
L1 = locate[0]
L2 = locate[1]
dist1 = abs(A-L1) + abs(B-L2)
dist2 = abs(B-L1) + abs(A-L2)
if dist1>dist2:
res += dist2
locate = [B,A]
else:
res += dist1
locate = [A,B]
print(res)
``` | instruction | 0 | 41,277 | 9 | 82,554 |
Yes | output | 1 | 41,277 | 9 | 82,555 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
D={}
for i in range(len(a)):
try:
D[a[i]] +=[i]
except:
D[a[i]]= [i]
#print(D)
steps=0
for i in D.keys():
try:
steps += abs(D[i][0]-D[i+1][0])+ abs(D[i][1]-D[i+1][1])
except:
pass
steps +=D[1][0]+D[1][1]
print(steps)
``` | instruction | 0 | 41,278 | 9 | 82,556 |
Yes | output | 1 | 41,278 | 9 | 82,557 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
from operator import itemgetter
n=int(input())
listing=list(input().split())
dictionary={}
counter=1
for i in listing:
if i not in dictionary:
dictionary[i]=[]
dictionary[i].append(counter)
counter+=1
prev_temp1=1
prev_temp2=1
cost=0
for i in range(1,n+1):
i=str(i)
cost+=abs(min(dictionary[i])-prev_temp1)+abs(max(dictionary[i])-prev_temp2)
prev_temp1=min(dictionary[i])
prev_temp2=max(dictionary[i])
print(cost)
``` | instruction | 0 | 41,279 | 9 | 82,558 |
Yes | output | 1 | 41,279 | 9 | 82,559 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
from math import ceil
n = int(input())
idx = {}
num= list(map(int,input().split()))
for i in range(2*n):
elem = num[i]
if elem in idx:
idx[elem]+=[i]
else:
idx[elem]=[i]
ans = 0
for i in range(1,n):
a = idx[i]
b = idx[i+1]
if i==1:
ans+=sum(a)
ans+=min(abs(a[0]-b[0])+abs(a[1]-b[1]),abs(a[0]-b[1])+abs(a[1]-b[0]))
print(ans)
``` | instruction | 0 | 41,280 | 9 | 82,560 |
No | output | 1 | 41,280 | 9 | 82,561 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
def find(arr,n):
dic= {}
for i in range(len(arr)):
if arr[i] in dic:
dic[arr[i]].append(i)
else:
dic[arr[i]] = [i]
cp =cd = res = 0
for i in range(1,n+1):
k = dic[i]
if i == 1:
cs = k[0]
cd = k[1]
res+=cs+cd
else:
if abs(k[0]-cs) <= abs(k[1]-cs):
res+=abs(k[0]-cs)
cs = k[0]
res+=abs(k[1]-cd)
cd = k[1]
else:
res+=abs(k[1]-cs)
cs = k[1]
res+=abs(k[0]-cd)
cd = k[0]
return res
def main():
n = int(input())
arr = list(map(int,input().split()))
print(find(arr,n))
if __name__ == "__main__":
main()
``` | instruction | 0 | 41,281 | 9 | 82,562 |
No | output | 1 | 41,281 | 9 | 82,563 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
t = [[] for i in range(n + 1)]
t[0] = [0, 0]
for i in range(2 * n):
t[a[i]].append(i)
su = 0
print(t)
for i in range(1, n + 1):
su += abs(t[i][0] - t[i - 1][0]) + abs(t[i][1] - t[i - 1][1])
print(su)
``` | instruction | 0 | 41,282 | 9 | 82,564 |
No | output | 1 | 41,282 | 9 | 82,565 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sasha and Dima want to buy two n-tier cakes. Each cake should consist of n different tiers: from the size of 1 to the size of n. Tiers should go in order from the smallest to the biggest (from top to bottom).
They live on the same street, there are 2 β
n houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the i-th house you can buy a tier of the size a_i (1 β€ a_i β€ n).
Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: 1, then 2, then 3 and so on up to n.
Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly 1.
Input
The first line of the input contains an integer number n β the number of tiers in each cake (1 β€ n β€ 10^5).
The second line contains 2 β
n integers a_1, a_2, ..., a_{2n} (1 β€ a_i β€ n), where a_i is equal to the size of the tier, which can be bought in the i-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 1 to n occurs in a exactly two times.
Output
Print one number β the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy n tiers in ascending order of their sizes.
Examples
Input
3
1 1 2 2 3 3
Output
9
Input
2
2 1 1 2
Output
5
Input
4
4 1 3 2 2 3 1 4
Output
17
Note
In the first example, the possible optimal sequence of actions is:
* Sasha buys a tier of size 1 near the 1-st house (a_1=1);
* Dima goes to the house 2;
* Dima buys a tier of size 1 near the 2-nd house (a_2=1);
* Sasha goes to the house 4;
* Sasha buys a tier of size 2 near the 4-th house (a_4=2);
* Sasha goes to the house 5;
* Sasha buys a tier of size 3 near the 5-th house (a_5=3);
* Dima goes to the house 3;
* Dima buys a tier of size 2 near the 3-rd house (a_3=2);
* Dima goes to the house 6;
* Dima buys a tier of size 3 near the 6-th house (a_6=3).
So, Sasha goes the distance 3+1=4, and Dima goes the distance 1+1+3=5. In total, they cover a distance of 4+5=9. You can make sure that with any other sequence of actions they will walk no less distance.
Submitted Solution:
```
n = int(input())
pieces = list(map(int, input().split()))
steps = 0
numbers_pos = {}
for i in range(n*2):
if pieces[i] not in numbers_pos:
numbers_pos[pieces[i]] = [i]
else:
numbers_pos[pieces[i]].append(i)
steps = 0
counter = 1
pos = 0
for i in range(n):
if abs(numbers_pos[counter][0] - pos) < abs(numbers_pos[counter][1] - pos):
numbers_pos[counter].reverse()
steps += abs(numbers_pos[counter][1] - pos)
pos = numbers_pos[counter][1]
numbers_pos[counter].pop()
counter += 1
counter = 1
pos = 0
for i in range(n):
steps += abs(numbers_pos[counter][0] - pos)
pos = numbers_pos[counter][0]
numbers_pos[counter].pop()
counter += 1
print(steps)
``` | instruction | 0 | 41,283 | 9 | 82,566 |
No | output | 1 | 41,283 | 9 | 82,567 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,317 | 9 | 82,634 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
import sys
def find(u,a):
if a[u]!=-1:
a[u]=find(a[u],a)
return a[u]
else:
return u
def union(a,u,v,ct):
x=find(u,a)
y=find(v,a)
#print(x,y)
if x==y:
ct+=1
else:
a[x]=y
ct=ct
return ct
n,k=map(int,input().split())
a=[-1 for i in range(n+1)]
count=0
for i in range(k):
u,v=map(int,input().split())
count=union(a,u,v,count)
#print(a)
print(count)
``` | output | 1 | 41,317 | 9 | 82,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,318 | 9 | 82,636 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
# from debug import debug
import sys;input = sys.stdin.readline
from collections import deque
n,m = map(int, input().split())
graph = [[] for i in range(n)]
for _ in range(m):
a,b = map(int, input().split())
graph[a-1].append(b-1)
graph[b-1].append(a-1)
ans = 0
v = [False]*n
for i in range(n):
if not v[i]:
size = 0
q = deque([i])
v[i] = True
while q:
node = q.popleft()
size+=1
for i in graph[node]:
if not v[i]:
v[i] = True
q.append(i)
ans += size-1
print(m-ans)
``` | output | 1 | 41,318 | 9 | 82,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,319 | 9 | 82,638 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
n,m = [int(j) for j in input().split()]
adj = [set() for i in range(n)]
for i in range(m):
l, r = [int(j)-1 for j in input().split()]
adj[l].add(r)
adj[r].add(l)
ct = 0
# print(adj)
visited = [False for i in range(n)]
for i in range(n):
if visited[i]==False:
stack = [i]
tct = 1
while(stack):
p = stack.pop()
visited[p]=True
# print(p, "iska bs", visited)
for j in adj[p]:
if visited[j]==False:
# print
# print(j, "andar")
visited[j]=True
tct+=1
stack.append(j)
# print(tct-1)
ct += max(0, tct-1)
print(m-ct)
# print(ans)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 41,319 | 9 | 82,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,320 | 9 | 82,640 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import deque
N, K = map(int, input().split())
X = [[] for i in range(N)]
for i in range(K):
x, y = map(int, input().split())
X[x-1].append(y-1)
X[y-1].append(x-1)
mi = 10**6
mii = 0
for i in range(N):
if len(X) and (len(X) < mi):
mi = len(X)
mii = i
Y = [(len(X[i]), i) for i in range(N) if len(X[i])]
Y = sorted(Y)
Q = [y[1] for y in Y]
P = [-1] * N
D = [0] * N
ans = 0
while Q:
i = Q.pop()
for a in X[i]:
if D[a] == 0 or D[i] == 0:
ans += 1
D[a] = 1
D[i] = 1
if a != P[i]:
P[a] = i
X[a].remove(i)
Q.append(a)
print(K - ans)
``` | output | 1 | 41,320 | 9 | 82,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,321 | 9 | 82,642 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
# ------------------- fast io --------------------
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")
# ------------------- fast io --------------------
from collections import defaultdict,deque
n,k=map(int,input().split());graph=[set([]) for s in range(n)]
ind=defaultdict(list);inverse={}
for s in range(k):
x,y=map(int,input().split());x-=1;y-=1
graph[x].add(y);graph[y].add(x)
ind[frozenset([x,y])].append(s);inverse[s]=[x,y]
ans=[];vis=set([]);done=set([])
for s in range(n):
if not(s in vis):
queue=deque([s]);vis.add(s)
while len(queue)>0:
v0=queue.popleft()
for i in graph[v0]:
if not(i in vis):
queue.append(i);vis.add(i)
gu=ind[frozenset([v0,i])].pop();ans.append(gu);done.add(gu)
for s in range(k):
if not(s in done):
ans.append(s)
unhappy=0;snacks=set([s for s in range(n)])
for s in range(len(ans)):
s1=inverse[ans[s]][0];s2=inverse[ans[s]][1]
if s1 in snacks or s2 in snacks:
snacks.discard(s1);snacks.discard(s2)
else:
unhappy+=1
print(unhappy)
``` | output | 1 | 41,321 | 9 | 82,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,322 | 9 | 82,644 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
from sys import stdin
n, k = tuple(int(x) for x in stdin.readline().split())
dic = {}
ans = 0
for i in range(k):
a, b = tuple(int(x) for x in stdin.readline().split())
a -= 1
b -= 1
if a not in dic:
dic[a] = set((b,))
else:
dic[a].add(b)
if b not in dic:
dic[b] = set((a,))
else:
dic[b].add(a)
for i in range(n):
if i in dic:
lst = [i]
s = set((i,))
for src in lst:
if src in dic:
for dest in dic[src]:
if dest in dic and dest not in s:
lst.append(dest)
s.add(dest)
ans += 1
del dic[src]
print(k-ans)
``` | output | 1 | 41,322 | 9 | 82,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,323 | 9 | 82,646 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
#USING DFS TO GROUP SNACKS
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
import sys
input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
from collections import defaultdict as d
#The goal here is to get as many animals to eat 1 snack.
#1st guest in a cycle must eat 2 snacks. Remaining snack can always be eaten
#by 1 guest (if there are enough guests)
@bootstrap
def dfs(node,root):
groupSnackSizes[root]+=1
snackRoot[node]=root
for nex in snackAdj[node]:
if snackRoot[nex]==-1: #unvisited
yield dfs(nex,root)
yield None
n,k=[int(x) for x in input().split()]
guests=[]
snackAdj=[set() for _ in range(n)]
for _ in range(k):
x,y=[int(z) for z in input().split()]
x-=1;y-=1
snackAdj[x].add(y)
snackAdj[y].add(x)
guests.append([x,y])
roots=set()
snackRoot=[-1 for _ in range(n)]
groupSnackSizes=d(lambda:0) #groupSnackSizes[root]=number of snacks in group
for i in range(n):
if snackRoot[i]==-1: #unvisited
roots.add(i)
dfs(i,i)
groupGuestSizes=d(lambda:0) #groupGuestSizes[root]=number of guests in group
for x,y in guests:
groupGuestSizes[snackRoot[x]]+=1
ans=k
for root in roots:
#nHappy guests is either number of guests or number of snacks-1 whichever is smaller
ans-=min(groupGuestSizes[root],groupSnackSizes[root]-1)
print(ans)
``` | output | 1 | 41,323 | 9 | 82,647 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied. | instruction | 0 | 41,324 | 9 | 82,648 |
Tags: dfs and similar, dsu, graphs
Correct Solution:
```
from queue import Queue
"""
def dfs(graphs, visited, here):
if visited[here] == False:
visited[here] = True
for next in graphs[here]:
if visited[next] == False:
dfs(graphs, visited, next)
def dfsAll(graphs, visited, N):
ret = 0
for here in range(1, N+1):
if visited[here] == False:
ret += 1
dfs(graphs, visited, here)
return ret
"""
"""
def bfs(graphs, visited, here):
if visited[here] == False:
q = Queue()
q.put(here)
while q.qsize() > 0:
here = q.get()
for next in graphs[here]:
if visited[next] == False:
visited[next] = True
q.put(next)
def bfsAll(graphs, visited, N):
ret = 0
for here in range(1, N+1):
if visited[here] == False:
ret += 1
bfs(graphs, visited, here)
return ret
"""
N, K = map(int,input().strip().split(' '))
snacks = [[] for i in range(N+1)]
visited = [False for i in range(N+1)]
for k in range(K):
a, b = map(int,input().strip().split(' '))
snacks[a].append(b)
snacks[b].append(a)
q = Queue()
components = 0
for here in range(1, N+1):
if visited[here] == False:
visited[here] = True
for there in snacks[here]:
if visited[there] == False:
visited[there] = True
q.put(there)
while q.qsize() > 0:
here = q.get()
for there in snacks[here]:
if visited[there] == False:
visited[there] = True
q.put(there)
components += 1
ret = K - (N - components)
print(ret)
``` | output | 1 | 41,324 | 9 | 82,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
# Question D. Cow and Snacks
class UnionFind():
def __init__(self, n):
self.count = n
self.parent = [i for i in range(n)]
def find(self, x):
if x != self.parent[x]:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
if px == py:
# x and y are in the same group
return False
else:
# let group x's ancestor be py, successfully union two groups
self.count -= 1
self.parent[px] = py
return True
def cowAndSnacks(fav, m):
uf = UnionFind(m)
sad = 0
for guest in fav:
[a, b] = fav[guest]
if uf.union(a, b) == False:
sad += 1
return sad
if __name__ == "__main__":
line = input()
[n, k] = list(map(int, line.split(" ")))
fav = {} # map guest to favourite snacks
snacks = {} # map original number to new indices
for guest in range(k):
f = list(map(int, input().split(" ")))
s = []
for snack in f:
if snack in snacks:
new_idx = snacks[snack]
else:
new_idx = len(snacks)
snacks[snack] = new_idx
s.append(new_idx)
fav[guest] = s
m = len(snacks)
res = cowAndSnacks(fav, m)
print(res)
``` | instruction | 0 | 41,325 | 9 | 82,650 |
Yes | output | 1 | 41,325 | 9 | 82,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
import sys
from collections import defaultdict,deque
n,k=map(int,sys.stdin.readline().split())
fav=[[0,0] for _ in range(k+1)]
sweet=defaultdict(list)
for i in range(1,k+1):
u,v=map(int,sys.stdin.readline().split())
fav[i]=[u,v]
sweet[u].append(i)
sweet[v].append(i)
#print(fav,'fav')
#print(sweet,'sweet')
vis=defaultdict(int)
viss=defaultdict(int)
ans=0
for i in range(1,k+1):
if vis[i]==0:
q=deque()
q.append(i)
vis[i]=1
while q:
a=q.pop()
#print(a,'a')
c,d=fav[a]
#print(a,'a',c,'c',d,'d',viss[c],'vissc',viss[d],'viss[d]')
if viss[c]==viss[d]==1:
ans+=1
continue
if viss[c]==0:
viss[c]=1
for j in sweet[c]:
if vis[j]==0:
q.append(j)
vis[j]+=1
if viss[d]==0:
viss[d]=1
for j in sweet[d]:
if vis[j]==0:
q.append(j)
vis[j]+=1
print(ans)
``` | instruction | 0 | 41,326 | 9 | 82,652 |
Yes | output | 1 | 41,326 | 9 | 82,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
def find(people,N):
dic={}
gr=[[] for _ in range(N+1)]
count=0
for (a,b) in people:
a,b=sorted([a,b])
if (a,b) in dic:
count+=1
else:
dic[(a,b)]=1
gr[a]+=[b]
gr[b]+=[a]
checked=[0]*(N+1)
parent=[-1]*(N+1)
for i in range(1,N+1):
if checked[i]==0:
stack=[i]
while stack:
node=stack.pop()
checked[node]=2
for v in gr[node]:
if parent[node]==v:
continue
if checked[v]==2:
continue
if checked[v]==1:
count+=1
else:
stack+=[v]
parent[v]=node
checked[v]=1
return count
people=[]
n,k=list(map(int,input().strip().split(" ")))
for _ in range(k):
a,b=list(map(int,input().strip().split(" ")))
people+=[(a,b)]
print(find(people,n))
``` | instruction | 0 | 41,327 | 9 | 82,654 |
Yes | output | 1 | 41,327 | 9 | 82,655 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
import sys
def main():
def input():
return sys.stdin.readline()[:-1]
N, k = map(int,input().split())
# union-find
parent = [k for k in range(N)]
def find(x):
if parent[x] == x:
return x
else:
parent[x] = find(parent[x])
return find(parent[x])
def unite(x,y):
parent[find(x)] = find(y)
ans = 0
for q in range(k):
x, y = map(int,input().split())
if find(x-1) == find(y-1):
ans += 1
else:
unite(x-1,y-1)
print(ans)
if __name__ == '__main__':
main()
``` | instruction | 0 | 41,328 | 9 | 82,656 |
Yes | output | 1 | 41,328 | 9 | 82,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
from collections import defaultdict
class UF:
"""An implementation of union find data structure.
It uses weighted quick union by rank with path compression.
"""
def __init__(self, N):
"""Initialize an empty union find object with N items.
Args:
N: Number of items in the union find object.
"""
self._id = list(range(N))
self._count = N
self._rank = [0] * N
def find(self, p):
"""Find the set identifier for the item p."""
id = self._id
while p != id[p]:
p = id[p] = id[id[p]] # Path compression using halving.
return p
def count(self):
"""Return the number of items."""
return self._count
def connected(self, p, q):
"""Check if the items p and q are on the same set or not."""
return self.find(p) == self.find(q)
def union(self, p, q):
"""Combine sets containing p and q into a single set."""
id = self._id
rank = self._rank
i = self.find(p)
j = self.find(q)
if i == j:
return
self._count -= 1
if rank[i] < rank[j]:
id[i] = j
elif rank[i] > rank[j]:
id[j] = i
else:
id[j] = i
rank[i] += 1
n, K = [int(i) for i in input().split()]
uf_store = UF(n)
component_dict = defaultdict(int)
for k in range(K):
x, y = [int(i)-1 for i in input().split()]
uf_store.union(x, y)
# I have constructed uf_store, now need to access their size of each component
for i in range(n):
component_dict[uf_store.find(i)] += 1
# i have constructed the size of each component
ans = 0
for j in component_dict:
ans += component_dict[j] - 1 #spanning tree size
print(K-ans)
``` | instruction | 0 | 41,329 | 9 | 82,658 |
No | output | 1 | 41,329 | 9 | 82,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
n,g=map(int,input().split())
l=[]
s=[]
for i in range(g):
r=list(map(int,input().split()))
l.append(r)
for i in range(1,n+1):
s.append(i)
for i in range(n):
for j in range(len(l)):
if s[i] in l[j]:
l.remove(l[j])
break
print(len(l))
``` | instruction | 0 | 41,330 | 9 | 82,660 |
No | output | 1 | 41,330 | 9 | 82,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10**5+10
MOD = 10**9+7
def isprime(n):
n = abs(int(n))
if n < 2:
return False
if n == 2:
return True
if not n & 1:
return False
for x in range(3, int(n**0.5) + 1, 2):
if n % x == 0:
return False
return True
def mhd(a,b):
return abs(a[0]-b[0])+abs(b[1]-a[1])
def charIN(x= ' '):
return(sys.stdin.readline().strip().split(x))
def arrIN(x = ' '):
return list(map(int,sys.stdin.readline().strip().split(x)))
def eld(x,y):
a = y[0]-x[0]
b = x[1]-y[1]
return (a*a+b*b)**0.5
def lgcd(a):
g = a[0]
for i in range(1,len(a)):
g = math.gcd(g,a[i])
return g
def ms(a):
msf = -MAX
meh = 0
st = en = be = 0
for i in range(len(a)):
meh+=a[i]
if msf<meh:
msf = meh
st = be
en = i
if meh<0:
meh = 0
be = i+1
return msf,st,en
def ncr(n,r):
num=den=1
for i in range(r):
num = (num*(n-i))%MOD
den = (den*(i+1))%MOD
return (num*(pow(den,MOD-2,MOD)))%MOD
def flush():
return sys.stdout.flush()
def fac(n):
ans = 1
for i in range(1,n+1):
ans*=i
ans%=MOD
return ans
'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
n,k = arrIN()
l = []
for _ in range(k):
x,y = arrIN()
if x>y:
x,y = y,x
l.append([x,y])
cnt1=cnt2=cnt3=cnt4=0
l.sort(key = lambda x:x[0])
s = set()
s.add(l[0][0])
s.add(l[0][1])
for i in range(1,k):
if (l[i][0] in s) and (l[i][1] in s):
cnt1+=1
else:
s.add(l[i][0])
s.add(l[i][1])
l.sort(key = lambda x:x[1])
s = set()
s.add(l[0][0])
s.add(l[0][1])
for i in range(1,k):
if (l[i][0] in s) and (l[i][1] in s):
cnt2+=1
else:
s.add(l[i][0])
s.add(l[i][1])
l.sort(key = lambda x:x[0],reverse=True)
s = set()
s.add(l[0][0])
s.add(l[0][1])
for i in range(1,k):
if (l[i][0] in s) and (l[i][1] in s):
cnt3+=1
else:
s.add(l[i][0])
s.add(l[i][1])
l.sort(key = lambda x:x[1],reverse=True)
s = set()
s.add(l[0][0])
s.add(l[0][1])
for i in range(1,k):
if (l[i][0] in s) and (l[i][1] in s):
cnt4+=1
else:
s.add(l[i][0])
s.add(l[i][1])
cnt5=cnt6=0
l.sort(key = lambda x:abs(x[0]-x[1]),reverse=True)
s = set()
s.add(l[0][0])
s.add(l[0][1])
for i in range(1,k):
if (l[i][0] in s) and (l[i][1] in s):
cnt5+=1
else:
s.add(l[i][0])
s.add(l[i][1])
l.sort(key = lambda x:abs(x[0]-x[1]))
s = set()
s.add(l[0][0])
s.add(l[0][1])
for i in range(1,k):
if (l[i][0] in s) and (l[i][1] in s):
cnt6+=1
else:
s.add(l[i][0])
s.add(l[i][1])
print(min(cnt1,cnt2,cnt3,cnt4,cnt5,cnt6))
``` | instruction | 0 | 41,331 | 9 | 82,662 |
No | output | 1 | 41,331 | 9 | 82,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!
There are n snacks flavors, numbered with integers 1, 2, β¦, n. Bessie has n snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows:
* First, Bessie will line up the guests in some way.
* Then in this order, guests will approach the snacks one by one.
* Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad.
Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
Input
The first line contains integers n and k (2 β€ n β€ 10^5, 1 β€ k β€ 10^5), the number of snacks and the number of guests.
The i-th of the following k lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), favorite snack flavors of the i-th guest.
Output
Output one integer, the smallest possible number of sad guests.
Examples
Input
5 4
1 2
4 3
1 4
3 4
Output
1
Input
6 5
2 3
2 1
3 4
6 5
4 5
Output
0
Note
In the first example, Bessie can order the guests like this: 3, 1, 2, 4. Guest 3 goes first and eats snacks 1 and 4. Then the guest 1 goes and eats the snack 2 only, because the snack 1 has already been eaten. Similarly, the guest 2 goes up and eats the snack 3 only. All the snacks are gone, so the guest 4 will be sad.
In the second example, one optimal ordering is 2, 1, 3, 5, 4. All the guests will be satisfied.
Submitted Solution:
```
n, k = map(int, input().split())
nums = [0 for _ in range(k)]
check = [0 for _ in range(n+1)]
for i in range(k):
nums[i] = list(map(int, input().split()))
nums.sort()
ans = 0
for num in nums:
first = num[0]
second = num[1]
if check[first] != 0 and check[second] != 0:
ans += 1
continue
if check[first] == 0:
check[first] = 1
if check[second] == 0:
check[second] = 1
print(ans)
``` | instruction | 0 | 41,332 | 9 | 82,664 |
No | output | 1 | 41,332 | 9 | 82,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,381 | 9 | 82,762 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t=int(input())
for i in range(t):
m,n,o=map(int,input().split())
a=max(m,n,o)
c=min(m,n,o)
b=m+n+o-a-c
d=0
if a!=0:
d+=1
a-=1
if b!=0:
d+=1
b-=1
if c!=0:
d+=1
c-=1
if a!=0 and b!=0:
d+=1
a-=1
b-=1
if a!=0 and c!=0:
d+=1
a-=1
c-=1
if b!=0 and c!=0:
d+=1
b-=1
c-=1
if a!=0 and b!=0 and c!=0:
d+=1
print(d)
d=0
``` | output | 1 | 41,381 | 9 | 82,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,382 | 9 | 82,764 |
Tags: brute force, greedy, implementation
Correct Solution:
```
def max_visitors(a,b,c):
count = 0
if a>0:
count+=1
a-=1
if b>0:
count+=1
b-=1
if c>0:
count+=1
c-=1
l = [a,b,c]
l.sort(reverse=True)
a = l[0]
b = l[1]
c = l[2]
if a and b:
count+=1
a-=1
b-=1
if a and c:
count+=1
a-=1
c-=1
if b and c:
count+=1
b-=1
c-=1
if a and b and c:
count+=1
a-=1
b-=1
c-=1
return count
t = int(input())
for _ in range(t):
abc = input().split()
a = int(abc[0])
b = int(abc[1])
c = int(abc[2])
print(max_visitors(a,b,c))
``` | output | 1 | 41,382 | 9 | 82,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,383 | 9 | 82,766 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import os
import sys
if os.path.exists('/mnt/c/Users/Square/square/codeforces'):
f = iter(open('A.txt').readlines())
def input():
return next(f)
# input = lambda: sys.stdin.readline().strip()
else:
input = lambda: sys.stdin.readline().strip()
fprint = lambda *args: print(*args, flush=True)
import itertools
t = int(input())
combos = [[0], [1], [2], [0, 1], [0, 2], [1, 2], [0, 1, 2]]
for _ in range(t):
a, b, c = map(int, input().split())
best = 0
for i in itertools.product(*[[0, 1] for _ in range(7)]):
res = [0, 0, 0]
for n, j in enumerate(i):
if j:
for k in combos[n]:
res[k] += 1
if all([u >= v for u, v in zip([a, b, c], res)]):
best = max(best, sum(i))
print(best)
``` | output | 1 | 41,383 | 9 | 82,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,384 | 9 | 82,768 |
Tags: brute force, greedy, implementation
Correct Solution:
```
maxServ = []
for _ in range(11 ** 3):
maxServ.append(0)
for elem in range(1 << 7):
dumpling = 0
cranberry = 0
pancake = 0
serve = 0
if elem & 1:
dumpling += 1
serve += 1
if elem & 1 << 1:
cranberry += 1
serve += 1
if elem & 1 << 2:
pancake += 1
serve += 1
if elem & 1 << 3:
dumpling += 1
cranberry += 1
serve += 1
if elem & 1 << 4:
dumpling += 1
pancake += 1
serve += 1
if elem & 1 << 5:
pancake += 1
cranberry += 1
serve += 1
if elem & 1 << 6:
dumpling += 1
pancake += 1
cranberry += 1
serve += 1
for i in range(dumpling,11):
for j in range(cranberry,11):
for k in range(pancake,11):
if maxServ[i * 121 + j * 11 + k] < serve:
maxServ[i * 121 + j * 11 + k] = serve
for _ in range(int(input())):
a,b,c = map(int,input().split())
print(maxServ[a * 121 + b * 11 + c])
``` | output | 1 | 41,384 | 9 | 82,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,385 | 9 | 82,770 |
Tags: brute force, greedy, implementation
Correct Solution:
```
for i in range(int(input())):
a,b,c=sorted(map(int,input().split()))
if(a>=4):
print(7)
elif(a==0 and b==0 and c==0):
print(0)
elif(a==0 and b==0 and c>0):
print(1)
elif(a==0 and b==1):
print(2)
elif(a==0 and b>1):
print(3)
elif(a==1 and b==1):
print(3)
elif(a==1 and b!=1):
print(4)
elif(a==2 and b==2 and c==2):
print(4)
elif(a==2 and c!=a):
print(5)
elif(a==3 and b==3 and c==3):
print(6)
elif(a==3 and c!=a):
print(6)
``` | output | 1 | 41,385 | 9 | 82,771 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,386 | 9 | 82,772 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t=int(input())
for i in range(0,t):
lst=list(map(int,input().split()))
c=0
lst.sort(reverse=True)
if lst[0]>0:
c+=1
lst[0]-=1
if lst[1]>0:
c+=1
lst[1]-=1
if lst[2]>0:
c+=1
lst[2]-=1
if lst[0]>0 and lst[1]>0:
c+=1
lst[0]-=1
lst[1]-=1
if lst[0]>0 and lst[2]>0:
c+=1
lst[0]-=1
lst[2]-=1
if lst[1]>0 and lst[2]>0:
c+=1
lst[2]-=1
lst[1]-=1
if lst[0]>0 and lst[1]>0 and lst[2]>0:
c+=1
print(c)
``` | output | 1 | 41,386 | 9 | 82,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,387 | 9 | 82,774 |
Tags: brute force, greedy, implementation
Correct Solution:
```
t = int(input())
while t!=0:
list1 = list(map(int,input().split()))
count=0
if list1[0]>0:
count+=1
list1[0]-=1
if list1[1]>0:
count+=1
list1[1]-=1
if list1[2]>0:
count+=1
list1[2]-=1
list1.sort()
if list1[-1]>0 and list1[0]>0:
count+=1
list1[-1]-=1
list1[0]-=1
if list1[-1]>0 and list1[1]>0:
count+=1
list1[-1]-=1
list1[1]-=1
if list1[1]>0 and list1[0]>0:
count+=1
list1[1]-=1
list1[0]-=1
if list1[-1]>0 and list1[1]>0 and list1[0]>0:
count+=1
list1[-1]-=1
list1[0]-=1
list1[1]-=1
print(count)
t-=1
``` | output | 1 | 41,387 | 9 | 82,775 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors. | instruction | 0 | 41,388 | 9 | 82,776 |
Tags: brute force, greedy, implementation
Correct Solution:
```
from collections import Counter
from collections import defaultdict
import math
t=int(input())
for _ in range(0,t):
a,b,c=list(map(int,input().split()))
s1=[a,b,c]
s1.sort(reverse=True)
l=['100','010','001','110','101','011','111']
ans=0
for i in l:
if(int(i[0])<=int(s1[0]) and int(i[1])<=int(s1[1]) and int(i[2])<=int(s1[2]) ):
s1[0]=s1[0]-int(i[0])
s1[1]=s1[1]-int(i[1])
s1[2]=s1[2]-int(i[2])
ans=ans+1
print(ans)
``` | output | 1 | 41,388 | 9 | 82,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
from itertools import product
pt = [bits for bits in product([0, 1], repeat=3)]
for _ in range(t):
a,b,c = map(int,input().split())
ans = 0
for bits in product([0, 1], repeat=8):
sum_b = [0, 0, 0]
for idx, bit in enumerate(bits):
if bit == 1:
sum_b[0] += pt[idx][0]
sum_b[1] += pt[idx][1]
sum_b[2] += pt[idx][2]
if sum_b[0] <= a and sum_b[1] <= b and sum_b[2] <= c:
ans = max(ans, sum(bits[1:]))
print(ans)
``` | instruction | 0 | 41,389 | 9 | 82,778 |
Yes | output | 1 | 41,389 | 9 | 82,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
t=int(input())
for i in range(t):
a=list(map(int,input().split()))
a.sort()
ans=0
for j in range(3):
if a[j]!=0:
a[j]-=1
ans += 1
if a[2]!=0 and a[0]!=0:
a[2]-=1
a[0]-=1
ans+=1
if a[2]!=0 and a[1]!=0:
a[2]-=1
a[1]-=1
ans+=1
if a[0]!=0 and a[1]!=0:
a[0]-=1
a[1]-=1
ans+=1
if min(a)>0:
ans+=1
print(ans)
``` | instruction | 0 | 41,390 | 9 | 82,780 |
Yes | output | 1 | 41,390 | 9 | 82,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
def solution():
from sys import stdout, stdin
_input, _print = stdin.readline, stdout.write
for _ in range(int(input())):
arr = [int(i) for i in input().split()]
arr.sort(reverse=True)
counter = 0
for i in range(3):
if arr[i] > 0:
arr[i] -= 1
counter += 1
if arr[i] > 0 and arr[(i+1)%3] > 0:
counter += 1
arr[i] -= 1
arr[(i+1)%3] -= 1
if min(arr) > 0:
counter += 1
print(counter)
solution()
``` | instruction | 0 | 41,391 | 9 | 82,782 |
Yes | output | 1 | 41,391 | 9 | 82,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
def solve():
l = [int(x) for x in input().split()]
l.sort()
ppl = 0
for i in range(3):
if l[i] != 0:
l[i] -= 1
ppl += 1
for j in range(3):
if l[j] != 0 and l[j-1] != 0:
l[j] -= 1
l[j-1] -= 1
ppl += 1
if l[0] != 0 and l[1] != 0 and l[2] != 0:
ppl += 1
print(ppl)
for _ in range(int(input())):
solve()
``` | instruction | 0 | 41,392 | 9 | 82,784 |
Yes | output | 1 | 41,392 | 9 | 82,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
for _ in range(int(input())):
a,b,c = map(int,input().split())
if a==b==c==0: print(0)
elif a==b==0 or b==c==0 or a==c==0: print(1)
elif (a==0 and min(b,c)==1) or (b==0 and min(a,c)==1) or (c==0 and min(a,c)==1): print(2)
elif (a==0 and min(b,c)==2) or (b==0 and min(a,c)==2) or (c==0 and min(a,c)==2): print(3)
elif a==b==1 or b==c==1 or a==c==1: print(3)
elif min(a,b,c)==1: print(4)
elif min(a,b,c)==2: print(5)
elif min(a,b,c)==3: print(6)
else: print(7)
``` | instruction | 0 | 41,393 | 9 | 82,786 |
No | output | 1 | 41,393 | 9 | 82,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
import sys
import math
def read_line():
return sys.stdin.readline()[:-1]
def read_int():
return int(sys.stdin.readline())
def read_int_line():
return [int(v) for v in sys.stdin.readline().split()]
t = read_int()
for i in range(t):
a,b,c = read_int_line()
ans = 0
if min(a,min(b,c))>=4:
print(7)
elif a>0 and b>0 and c>0:
# if a<4:
# ans -= 4-a
# if a==b:
# ans+=1
# if c==b:
# ans+=1
# if a==c:
# ans+=1
# if b<4:
# ans -= 4-b
# if a==b:
# ans+=1
# if c==b:
# ans+=1
# if a==c:
# ans+=1
# if c<4:
# ans -= 4-c
# if a==b:
# ans+=1
# if c==b:
# ans+=1
# if a==c:
# ans+=1
if min(a,min(b,c))>=1:
ans = 3
a-=1
b-=1
c-=1
l = [a,b,c]
l.sort(reverse = True)
a,b,c= l
if a>=1 and b>=1:
ans +=1
a-=1
b-=1
if c>=1 and b>=1:
ans +=1
c-=1
b-=1
if a>=1 and c>=1:
ans +=1
a-=1
c-=1
if a>=1 and b>=1 and c>=1:
ans +=1
print(ans)
else:
l = [a,b,c]
l.sort(reverse = True)
a,b,c= l
if b==0:
ans = 1
else:
if b==1:
ans = 2
else:
ans = 3
print(ans)
else:
print(0)
``` | instruction | 0 | 41,394 | 9 | 82,788 |
No | output | 1 | 41,394 | 9 | 82,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
"""
Satwik_Tiwari ;) .
4th july , 2020 - Saturday
"""
#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter # Counter(list) return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned
#==============================================================================================
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")
def print(*args, **kwargs):
"""Prints the values to a stream, or to sys.stdout by default."""
sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
at_start = True
for x in args:
if not at_start:
file.write(sep)
file.write(str(x))
at_start = False
file.write(kwargs.pop("end", "\n"))
if kwargs.pop("flush", False):
file.flush()
if sys.version_info[0] < 3:
sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
# inp = lambda: sys.stdin.readline().rstrip("\r\n")
#===============================================================================================
#some shortcuts
mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var)) #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n") #as stdout.write always print sring.
def testcase(t):
for p in range(t):
solve()
def printlist(a) :
for p in range(0,len(a)):
out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(a,b):
ans = 1
while(b>0):
if(b%2==1):
ans*=a
a*=a
b//=2
return ans
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) : # Check Prime Number or not
if (n <= 1) : return False
if (n <= 3) : return True
if (n % 2 == 0 or n % 3 == 0) : return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
#===============================================================================================
# code here ;))
def bs(a,l,h,x):
while(l<h):
# print(l,h)
mid = (l+h)//2
if(a[mid] == x):
return mid
if(a[mid] < x):
l = mid+1
else:
h = mid
return l
def sieve(a): #O(n loglogn) nearly linear
#all odd mark 1
for i in range(3,((10**6)+1),2):
a[i] = 1
#marking multiples of i form i*i 0. they are nt prime
for i in range(3,((10**6)+1),2):
for j in range(i*i,((10**6)+1),i):
a[j] = 0
a[2] = 1 #special left case
return (a)
def bfs(g,st):
visited = [-1]*(len(g))
visited[st] = 0
queue = []
queue.append(st)
new = []
while(len(queue) != 0):
s = queue.pop()
new.append(s)
for i in g[s]:
if(visited[i] == -1):
visited[i] = visited[s]+1
queue.append(i)
return visited
def dfsusingstack(v,st):
d = deque([])
visited = [0]*(len(v))
d.append(st)
new = []
visited[st] = 1
while(len(d) != 0):
curr = d.pop()
new.append(curr)
for i in v[curr]:
if(visited[i] == 0):
visited[i] = 1
d.append(i)
return new
def solve():
a,b,c = sep()
if(a>=3 and b>=3 and c>=3):
print(7)
return
ans = 0
if(a>0):
a-=1
ans+=1
if(b>0):
b-=1
ans+=1
if(c>0):
c-=1
ans+=1
if(a!=0 and b!=0 and c!=0):
if(a==1 and b==1 and c==1):
ans+=1
else:
ans+=2
else:
arr = [a,b,c]
arr = sorted(arr)
if(arr.count(0) == 1):
ans+=1
print(ans)
# testcase(1)
testcase(int(inp()))
``` | instruction | 0 | 41,395 | 9 | 82,790 |
No | output | 1 | 41,395 | 9 | 82,791 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Tired of boring office work, Denis decided to open a fast food restaurant.
On the first day he made a portions of dumplings, b portions of cranberry juice and c pancakes with condensed milk.
The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:
* every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);
* each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;
* all visitors should receive different sets of dishes.
What is the maximum number of visitors Denis can feed?
Input
The first line contains an integer t (1 β€ t β€ 500) β the number of test cases to solve.
Each of the remaining t lines contains integers a, b and c (0 β€ a, b, c β€ 10) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
Output
For each test case print a single integer β the maximum number of visitors Denis can feed.
Example
Input
7
1 2 1
0 0 0
9 1 7
2 2 3
2 3 2
3 2 2
4 4 4
Output
3
0
4
5
5
5
7
Note
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.
In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.
In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
Submitted Solution:
```
for _ in range(int(input())):
a,b,c=map(int,input().split())
s=[
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]]
count=0
l=[a,b,c]
l.sort()
a=l[0]
b=l[1]
c=l[2]
while a>0 and b>0 and c>0:
for i in range(len(s)):
a1=a-s[i][0]
b1=b-s[i][1]
c1=c-s[i][2]
if a1>=0 and b1>=0 and c1>=0:
count+=1
a=a-s[i][0]
b=b-s[i][1]
c=c-s[i][2]
print(count)
``` | instruction | 0 | 41,396 | 9 | 82,792 |
No | output | 1 | 41,396 | 9 | 82,793 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,433 | 9 | 82,866 |
Tags: greedy
Correct Solution:
```
n = int(input())
for i in range(n):
m = int(input())
s = input().split(' ')
s1 = input().split(' ')
a = []
b = []
for i in range(m):
a.append(int(s[i]))
b.append(int(s1[i]))
min1 = min(a)
min2 = min(b)
sum1 = 0
for i in range(m):
sum1 += max([a[i] - min1, b[i] - min2])
print(sum1)
``` | output | 1 | 41,433 | 9 | 82,867 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,434 | 9 | 82,868 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
amin = min(a)
bmin = min(b)
ans = 0
for i in range(n):
t = min(a[i]-amin, b[i]-bmin)
ans+=t
a[i]-=t
b[i]-=t
ans+=a[i]-amin
ans+=b[i]-bmin
print(ans)
``` | output | 1 | 41,434 | 9 | 82,869 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,435 | 9 | 82,870 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
candies = [int(x) for x in input().split()]
oranges= [int(x) for x in input().split()]
ans = 0
mc = min(candies)
mo = min(oranges)
for i in range(n):
if candies[i] == mc :
if oranges[i] == mo:
continue
else:
ans += oranges[i] - mo
elif oranges[i] == mo:
ans += candies[i] - mc
else :
candies[i] -= mc
oranges[i] -= mo
if candies[i] < oranges[i] :
ans += candies[i] + (oranges[i]-candies[i])
elif candies[i] > oranges[i] :
ans += oranges[i] + (candies[i]-oranges[i])
else:
ans += candies[i]
print(ans)
``` | output | 1 | 41,435 | 9 | 82,871 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,436 | 9 | 82,872 |
Tags: greedy
Correct Solution:
```
def main(n, a, b):
ans = 0
for i in range(n):
re_a = a[i] - min(a)
re_b = b[i] - min(b)
ans += max(re_a, re_b)
return ans
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = main(n, a, b)
print(ans)
``` | output | 1 | 41,436 | 9 | 82,873 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,437 | 9 | 82,874 |
Tags: greedy
Correct Solution:
```
t=int(input())
ans=[]
for k in range(t):
n=int(input())
p=input()
q=input()
a=list(map(int,p.split()))
b=list(map(int,q.split()))
x,y=min(a),min(b)
c=0
for i in range(n):
d1=a[i]-x
d2=b[i]-y
c+=max(d1,d2)
ans.append(c)
for i in ans:
print(i)
``` | output | 1 | 41,437 | 9 | 82,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,438 | 9 | 82,876 |
Tags: greedy
Correct Solution:
```
T = int(input())
for case in range(T):
N = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
a_min = min(a)
b_min = min(b)
moves = 0
for index in range(len(a)):
a_index = a[index] - a_min
b_index = b[index] - b_min
#print('a_index', a_index, 'b_index', b_index)
combined_moves = min(a_index, b_index)
#print('combined', combined_moves)
individual_moves = max(a_index - combined_moves, b_index - combined_moves)
#print('individual', individual_moves)
moves += combined_moves + individual_moves
print(moves)
``` | output | 1 | 41,438 | 9 | 82,877 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,439 | 9 | 82,878 |
Tags: greedy
Correct Solution:
```
# number of testcases as input
t = int(input())
# iterating till the range
for i in range(0, t):
n = int(input())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
amin = min(a)
bmin = min(b)
diff_array = [max(i-amin,j-bmin) for i, j in zip(a, b)]
print(sum(diff_array))
``` | output | 1 | 41,439 | 9 | 82,879 |
Provide tags and a correct Python 3 solution for this coding contest problem.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2]. | instruction | 0 | 41,440 | 9 | 82,880 |
Tags: greedy
Correct Solution:
```
for _ in range(int(input())):
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
x,y=min(a),min(b)
res=0
for i in range(n):
v1,v2=a[i]-x,b[i]-y
temp=min(v1,v2)
res+=temp
v1-=temp
v2-=temp
res+=v1+v2
print(res)
``` | output | 1 | 41,440 | 9 | 82,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You have n gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The i-th gift consists of a_i candies and b_i oranges.
During one move, you can choose some gift 1 β€ i β€ n and do one of the following operations:
* eat exactly one candy from this gift (decrease a_i by one);
* eat exactly one orange from this gift (decrease b_i by one);
* eat exactly one candy and exactly one orange from this gift (decrease both a_i and b_i by one).
Of course, you can not eat a candy or orange if it's not present in the gift (so neither a_i nor b_i can become less than zero).
As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: a_1 = a_2 = ... = a_n and b_1 = b_2 = ... = b_n (and a_i equals b_i is not necessary).
Your task is to find the minimum number of moves required to equalize all the given gifts.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1 β€ n β€ 50) β the number of gifts. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the number of candies in the i-th gift. The third line of the test case contains n integers b_1, b_2, ..., b_n (1 β€ b_i β€ 10^9), where b_i is the number of oranges in the i-th gift.
Output
For each test case, print one integer: the minimum number of moves required to equalize all the given gifts.
Example
Input
5
3
3 5 6
3 2 3
5
1 2 3 4 5
5 4 3 2 1
3
1 1 1
2 2 2
6
1 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1
3
10 12 8
7 5 4
Output
6
16
0
4999999995
7
Note
In the first test case of the example, we can perform the following sequence of moves:
* choose the first gift and eat one orange from it, so a = [3, 5, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 4, 6] and b = [2, 2, 3];
* choose the second gift and eat one candy from it, so a = [3, 3, 6] and b = [2, 2, 3];
* choose the third gift and eat one candy and one orange from it, so a = [3, 3, 5] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 4] and b = [2, 2, 2];
* choose the third gift and eat one candy from it, so a = [3, 3, 3] and b = [2, 2, 2].
Submitted Solution:
```
import sys
def equalize(apples, oranges):
a = min(apples)
b = min(oranges)
eat = 0
for i in range(len(apples)):
eat += max(apples[i]-a, oranges[i]-b)
return eat
t = int(sys.stdin.readline())
for i in range(t):
n = int(sys.stdin.readline())
apples = list(map(int, sys.stdin.readline().split()))
oranges = list(map(int, sys.stdin.readline().split()))
print(equalize(apples, oranges))
``` | instruction | 0 | 41,441 | 9 | 82,882 |
Yes | output | 1 | 41,441 | 9 | 82,883 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.