message
stringlengths
2
30.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
237
109k
cluster
float64
10
10
__index_level_0__
int64
474
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money.
instruction
0
91,069
10
182,138
Tags: greedy, math Correct Solution: ``` t=int(input()) for _ in range(t): x,y=map(int,input().split()) a,b=map(int,input().split()) if x<y: print(x*min(b,a+a) + (y-x)*a) else: print(y*min(b,a+a) + (x-y)*a) ```
output
1
91,069
10
182,139
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money.
instruction
0
91,070
10
182,140
Tags: greedy, math Correct Solution: ``` mod=998244353 t=int(input()) for _ in range(t): x,y=map(int,input().split()) a,b=map(int,input().split()) x1=(abs(x-y))*a + b*min(abs(x),abs(y)) x2=(x+y)*a print(min(x1,x2)) ```
output
1
91,070
10
182,141
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money.
instruction
0
91,071
10
182,142
Tags: greedy, math Correct Solution: ``` t = int(input()) for i in range(t): x, y = map(int, input().split()) a, b = map(int, input().split()) ans = 0 diff = abs(x-y) if b > 2*a: ans += x*a+y*a else: ans += diff*a + min(x, y)*b print(ans) ```
output
1
91,071
10
182,143
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money.
instruction
0
91,072
10
182,144
Tags: greedy, math Correct Solution: ``` if __name__ == "__main__": for _ in range(int(input())): x, y = map(int, input().split()) a, b = map(int, input().split()) b = min(b, a + a) if x < y: x, y= y, x print(y * b + (x - y) * a) ```
output
1
91,072
10
182,145
Provide tags and a correct Python 3 solution for this coding contest problem. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money.
instruction
0
91,073
10
182,146
Tags: greedy, math Correct Solution: ``` T = int(input()) for t in range(T): x, y = list(map(int, input().split())) a, b = list(map(int, input().split())) if(2*a <= b): print((abs(x)+abs(y))*a) continue else: ans = min(x, y)*b x, y =x-min(x,y), y- min(x,y) ans += max(x,y)*a print(ans) ```
output
1
91,073
10
182,147
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` t=int(input()) for _ in range(t): x,y=map(int,input().split()) a,b=map(int,input().split()) diff=abs(x-y) m=min(x,y) print(diff*a+min(m*a*2,m*b)) ```
instruction
0
91,074
10
182,148
Yes
output
1
91,074
10
182,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` a=int(input()) for i in range(0,a): x,y=map(int,input().split()) a,b=map(int,input().split()) minn=min(x,y) if(b>2*a): print(x*a+y*a) else: print(a*(abs(y-x))+b*minn) ```
instruction
0
91,075
10
182,150
Yes
output
1
91,075
10
182,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` t=int(input()) for i in range(t): num1=list(map(int,input().split())) num2=list(map(int,input().split())) x=num1[0] y=num1[1] a=num2[0] b=num2[1] z=(x+y)*a c=min(x,y) x-=c y-=c m=(c*b)+(x*a)+(y*a) print(min(m,z)) ```
instruction
0
91,076
10
182,152
Yes
output
1
91,076
10
182,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` for _ in range(int(input())): x,y=list(map(int,input().split())) a,b=list(map(int,input().split())) ans1=(x+y)*a if(x<y): temp=x t=y-x else: temp=y t=x-y ans2=temp*b+t*a if(ans1<ans2): print(ans1) else: print(ans2) ```
instruction
0
91,077
10
182,154
Yes
output
1
91,077
10
182,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` t=int(input()) for i in range(t): x,y=map(int,input().split()) a,b=map(int,input().split()) print(min(x,y)*b+(max(x,y)-min(x,y))*a) ```
instruction
0
91,078
10
182,156
No
output
1
91,078
10
182,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` t = int(input()) for _ in range(t): x,y = map(int,input().split(' ')) a,b = map(int,input().split(' ')) cost = 0 cost += min(x,y) * b cost += abs(x-y) * a print(cost) ```
instruction
0
91,079
10
182,158
No
output
1
91,079
10
182,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` for _ in range(int(input())): x,y = map(int,input().split()) a,b = map(int,input().split()) k=x*a+x*b if(x>y): l=x-y print(min((b*l+a),k)) elif(x<y): l=y-x print(min((a*l+b),k)) else: print(min(y*b,2*x*a)) ```
instruction
0
91,080
10
182,160
No
output
1
91,080
10
182,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given two integers x and y. You can perform two types of operations: 1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation: * x = 0, y = 6; * x = 0, y = 8; * x = -1, y = 7; * x = 1, y = 7. 2. Pay b dollars and increase or decrease both integers by 1. For example, if x = 0 and y = 7 there are two possible outcomes after this operation: * x = -1, y = 6; * x = 1, y = 8. Your goal is to make both given integers equal zero simultaneously, i.e. x = y = 0. There are no other requirements. In particular, it is possible to move from x=1, y=0 to x=y=0. Calculate the minimum amount of dollars you have to spend on it. Input The first line contains one integer t (1 ≀ t ≀ 100) β€” the number of testcases. The first line of each test case contains two integers x and y (0 ≀ x, y ≀ 10^9). The second line of each test case contains two integers a and b (1 ≀ a, b ≀ 10^9). Output For each test case print one integer β€” the minimum amount of dollars you have to spend. Example Input 2 1 3 391 555 0 0 9 4 Output 1337 0 Note In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars. In the second test case both integers are equal to zero initially, so you dont' have to spend money. Submitted Solution: ``` for i in range(int(input())): a=list(map(int,input().strip().split()))[:2] b=list(map(int,input().strip().split()))[:2] if a[0]>a[1]: count=(a[0]-a[1])*b[0]+(a[1])*b[1] print(count) elif a[0]<a[1]: count=(a[1]-a[0])*b[0]+a[0]*b[1] print(count) else: count=a[0]*b[1] print(count) ```
instruction
0
91,081
10
182,162
No
output
1
91,081
10
182,163
Provide tags and a correct Python 3 solution for this coding contest problem. We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor. There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people. After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too. Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer. Input The first line of the input contains two integers n and k (1 ≀ n ≀ 500 000, 0 ≀ k ≀ 109) β€” the number of citizens in Kekoland and the number of days left till Robin Hood's retirement. The second line contains n integers, the i-th of them is ci (1 ≀ ci ≀ 109) β€” initial wealth of the i-th person. Output Print a single line containing the difference between richest and poorest peoples wealth. Examples Input 4 1 1 1 4 2 Output 2 Input 3 1 2 2 2 Output 0 Note Lets look at how wealth changes through day in the first sample. 1. [1, 1, 4, 2] 2. [2, 1, 3, 2] or [1, 2, 3, 2] So the answer is 3 - 1 = 2 In second sample wealth will remain the same for each person.
instruction
0
91,327
10
182,654
Tags: binary search, greedy Correct Solution: ``` import sys sys.stderr = sys.stdout def hood(n, k, C): C.sort() m, r = divmod(sum(C), n) m1 = (m + 1) if r else m c_lo = C[0] k_lo = k for i, c in enumerate(C): if c_lo == m: break c_m = min(c, m) dc = c_m - c_lo dk = i * dc if k_lo >= dk: k_lo -= dk c_lo = c_m else: dc = k_lo // i c_lo += dc break c_hi = C[-1] k_hi = k for i, c in enumerate(reversed(C)): if c_hi == m1: break c_m1 = max(c, m1) dc = c_hi - c_m1 dk = i * dc if k_hi >= dk: k_hi -= dk c_hi = c_m1 else: dc = k_hi // i c_hi -= dc break return c_hi - c_lo def main(): n, k = readinti() C = readintl() print(hood(n, k, C)) ########## def readint(): return int(input()) def readinti(): return map(int, input().split()) def readintt(): return tuple(readinti()) def readintl(): return list(readinti()) def readinttl(k): return [readintt() for _ in range(k)] def readintll(k): return [readintl() for _ in range(k)] def log(*args, **kwargs): print(*args, **kwargs, file=sys.__stderr__) if __name__ == '__main__': main() ```
output
1
91,327
10
182,655
Provide tags and a correct Python 3 solution for this coding contest problem. In Ann's favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems. Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price. Ann decided that she wants to buy such non-empty subsegment that the sale operates on it and the number of math problems is greater than the number of economics problems exactly by k. Note that k may be positive, negative or zero. Unfortunately, Ann is not sure on which segment the sale operates, but she has q assumptions. For each of them she wants to know the number of options to buy a subsegment satisfying the condition (because the time she spends on choosing depends on that). Currently Ann is too busy solving other problems, she asks you for help. For each her assumption determine the number of subsegments of the given segment such that the number of math problems is greaten than the number of economics problems on that subsegment exactly by k. Input The first line contains two integers n and k (1 ≀ n ≀ 100 000, - 109 ≀ k ≀ 109) β€” the number of books and the needed difference between the number of math problems and the number of economics problems. The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 2), where ti is 1 if the i-th book is on math or 2 if the i-th is on economics. The third line contains n integers a1, a2, ..., an (0 ≀ ai ≀ 109), where ai is the number of problems in the i-th book. The fourth line contains a single integer q (1 ≀ q ≀ 100 000) β€” the number of assumptions. Each of the next q lines contains two integers li and ri (1 ≀ li ≀ ri ≀ n) describing the i-th Ann's assumption. Output Print q lines, in the i-th of them print the number of subsegments for the i-th Ann's assumption. Examples Input 4 1 1 1 1 2 1 1 1 1 4 1 2 1 3 1 4 3 4 Output 2 3 4 1 Input 4 0 1 2 1 2 0 0 0 0 1 1 4 Output 10 Note In the first sample Ann can buy subsegments [1;1], [2;2], [3;3], [2;4] if they fall into the sales segment, because the number of math problems is greater by 1 on them that the number of economics problems. So we should count for each assumption the number of these subsegments that are subsegments of the given segment. Segments [1;1] and [2;2] are subsegments of [1;2]. Segments [1;1], [2;2] and [3;3] are subsegments of [1;3]. Segments [1;1], [2;2], [3;3], [2;4] are subsegments of [1;4]. Segment [3;3] is subsegment of [3;4].
instruction
0
91,381
10
182,762
Tags: data structures, flows, hashing 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 math import ceil def prod(a, mod=10 ** 9 + 7): ans = 1 for each in a: ans = (ans * each) % mod return ans def gcd(x, y): while y: x, y = y, x % y return x def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if not True else 1): #n = int(input()) n, k = map(int, input().split()) # a, b = map(int, input().split()) # c, d = map(int, input().split()) t = list(map(int, input().split())) a = list(map(int, input().split())) # s = input() for i in range(n): # math = eco + k # math - eco = k if t[i] == 2: a[i] *= -1 count = [0] * (n + 1) pre = [0] for i in a: pre += [pre[-1] + i] index = {} cc = list(set(pre)) for i in range(len(cc)): index[cc[i]] = i minusK = [-1]*(n + 1) plusK = [-1] * (n + 1) zero = [-1] * (n + 1) for i in range(n + 1): if pre[i] - k in index: minusK[i] = index[pre[i] - k] if pre[i] + k in index: plusK[i] = index[pre[i] + k] zero[i] = index[pre[i]] BLOCK_SIZE = 320 blocks = [[] for i in range(BLOCK_SIZE)] q = int(input()) ans = [0]*q for i in range(q): l, r = map(int, input().split()) blocks[l // BLOCK_SIZE] += [[l-1, r, i]] for i in range(len(blocks)): if not blocks[i]: continue blocks[i] = sorted(blocks[i], key=lambda x: x[1]) left = right = BLOCK_SIZE * i res = 0 count[zero[left]] += 1 for l, r, ind in blocks[i]: while right < r: right += 1 if minusK[right] != -1: res += count[minusK[right]] count[zero[right]] += 1 while left < l: count[zero[left]] -= 1 if plusK[left] != -1: res -= count[plusK[left]] left += 1 while left > l: left -= 1 if plusK[left] != -1: res += count[plusK[left]] count[zero[left]] += 1 ans[ind] = res while left <= right: count[zero[left]] -= 1 if plusK[left] != -1: res -= count[plusK[left]] left += 1 assert res == 0 for i in ans: print(i) ```
output
1
91,381
10
182,763
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,158
10
184,316
Tags: binary search, greedy, two pointers Correct Solution: ``` #### IMPORTANT LIBRARY #### ############################ ### DO NOT USE import random --> 250ms to load the library ############################ ### In case of extra libraries: https://github.com/cheran-senthil/PyRival ###################### ####### IMPORT ####### ###################### from functools import cmp_to_key from collections import deque, Counter from heapq import heappush, heappop from math import log, ceil ###################### #### STANDARD I/O #### ###################### import sys import os 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") 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) def print(*args, **kwargs): 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() def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def ii(): return int(inp()) def si(): return str(inp()) def li(lag = 0): l = list(map(int, inp().split())) if lag != 0: for i in range(len(l)): l[i] += lag return l def mi(lag = 0): matrix = list() for i in range(n): matrix.append(li(lag)) return matrix def lsi(): #string list return list(map(str, inp().split())) def print_list(lista, space = " "): print(space.join(map(str, lista))) ###################### ### BISECT METHODS ### ###################### def bisect_left(a, x): """i tale che a[i] >= x e a[i-1] < x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] < x: left = mid+1 else: right = mid return left def bisect_right(a, x): """i tale che a[i] > x e a[i-1] <= x""" left = 0 right = len(a) while left < right: mid = (left+right)//2 if a[mid] > x: right = mid else: left = mid+1 return left def bisect_elements(a, x): """elementi pari a x nell'Γ‘rray sortato""" return bisect_right(a, x) - bisect_left(a, x) ###################### ### MOD OPERATION #### ###################### MOD = 10**9 + 7 maxN = 5 FACT = [0] * maxN INV_FACT = [0] * maxN def add(x, y): return (x+y) % MOD def multiply(x, y): return (x*y) % MOD def power(x, y): if y == 0: return 1 elif y % 2: return multiply(x, power(x, y-1)) else: a = power(x, y//2) return multiply(a, a) def inverse(x): return power(x, MOD-2) def divide(x, y): return multiply(x, inverse(y)) def allFactorials(): FACT[0] = 1 for i in range(1, maxN): FACT[i] = multiply(i, FACT[i-1]) def inverseFactorials(): n = len(INV_FACT) INV_FACT[n-1] = inverse(FACT[n-1]) for i in range(n-2, -1, -1): INV_FACT[i] = multiply(INV_FACT[i+1], i+1) def coeffBinom(n, k): if n < k: return 0 return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n-k])) ###################### #### GRAPH ALGOS ##### ###################### # ZERO BASED GRAPH def create_graph(n, m, undirected = 1, unweighted = 1): graph = [[] for i in range(n)] if unweighted: for i in range(m): [x, y] = li(lag = -1) graph[x].append(y) if undirected: graph[y].append(x) else: for i in range(m): [x, y, w] = li(lag = -1) w += 1 graph[x].append([y,w]) if undirected: graph[y].append([x,w]) return graph def create_tree(n, unweighted = 1): children = [[] for i in range(n)] if unweighted: for i in range(n-1): [x, y] = li(lag = -1) children[x].append(y) children[y].append(x) else: for i in range(n-1): [x, y, w] = li(lag = -1) w += 1 children[x].append([y, w]) children[y].append([x, w]) return children def dist(tree, n, A, B = -1): s = [[A, 0]] massimo, massimo_nodo = 0, 0 distanza = -1 v = [-1] * n while s: el, dis = s.pop() if dis > massimo: massimo = dis massimo_nodo = el if el == B: distanza = dis for child in tree[el]: if v[child] == -1: v[child] = 1 s.append([child, dis+1]) return massimo, massimo_nodo, distanza def diameter(tree): _, foglia, _ = dist(tree, n, 0) diam, _, _ = dist(tree, n, foglia) return diam def dfs(graph, n, A): v = [-1] * n s = [[A, 0]] v[A] = 0 while s: el, dis = s.pop() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges def bfs(graph, n, A): v = [-1] * n s = deque() s.append([A, 0]) v[A] = 0 while s: el, dis = s.popleft() for child in graph[el]: if v[child] == -1: v[child] = dis + 1 s.append([child, dis + 1]) return v #visited: -1 if not visited, otherwise v[B] is the distance in terms of edges #FROM A GIVEN ROOT, RECOVER THE STRUCTURE def parents_children_root_unrooted_tree(tree, n, root = 0): q = deque() visited = [0] * n parent = [-1] * n children = [[] for i in range(n)] q.append(root) while q: all_done = 1 visited[q[0]] = 1 for child in tree[q[0]]: if not visited[child]: all_done = 0 q.appendleft(child) if all_done: for child in tree[q[0]]: if parent[child] == -1: parent[q[0]] = child children[child].append(q[0]) q.popleft() return parent, children # CALCULATING LONGEST PATH FOR ALL THE NODES def all_longest_path_passing_from_node(parent, children, n): q = deque() visited = [len(children[i]) for i in range(n)] downwards = [[0,0] for i in range(n)] upward = [1] * n longest_path = [1] * n for i in range(n): if not visited[i]: q.append(i) downwards[i] = [1,0] while q: node = q.popleft() if parent[node] != -1: visited[parent[node]] -= 1 if not visited[parent[node]]: q.append(parent[node]) else: root = node for child in children[node]: downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse = True)[0:2] s = [node] while s: node = s.pop() if parent[node] != -1: if downwards[parent[node]][0] == downwards[node][0] + 1: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1]) else: upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0]) longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1 for child in children[node]: s.append(child) return longest_path ### TBD SUCCESSOR GRAPH 7.5 ### TBD TREE QUERIES 10.2 da 2 a 4 ### TBD ADVANCED TREE 10.3 ### TBD GRAPHS AND MATRICES 11.3.3 e 11.4.3 e 11.5.3 (ON GAMES) ###################### ## END OF LIBRARIES ## ###################### n,m,k,s = li() cd = li() ce = li() d = list() e = list() for i in range(m): t, v = li() if t == 1: d.append([v,i]) else: e.append([v,i]) d.sort() e.sort() prd = [0 for i in range(len(d)+1)] pre = [0 for i in range(len(e)+1)] for i in range(max(len(d), len(e))): if i < len(d): prd[i+1] = prd[i] + d[i][0] if i < len(e): pre[i+1] = pre[i] + e[i][0] md = list() me = list() mind = float("inf") didx = -1 mine = float("inf") eidx = -1 for i in range(n): if cd[i] < mind: mind = cd[i] didx = i if ce[i] < mine: mine = ce[i] eidx = i md.append([mind, didx]) me.append([mine, eidx]) left = 0 right = n compro = [-1, -1,-1,-1,-1] # min giorno, giorno doll, num doll, giorno eu, num eu flag = False while left < right: mid = (left+right)//2 currd, idxd = md[mid] curre, idxe = me[mid] for i in range(k+1): if i < len(prd) and k-i < len(pre): amount = currd*prd[i]+curre*pre[k-i] if amount <= s: flag = True compro = [mid+1, idxd+1,i,idxe+1,k-i] if flag == True: right = mid else: left = mid+1 flag = False if compro[0] != -1: print(compro[0]) for i in range(compro[2]): print_list([d[i][1]+1, compro[1]]) for j in range(compro[4]): print_list([e[j][1]+1, compro[3]]) else: print(-1) ```
output
1
92,158
10
184,317
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,159
10
184,318
Tags: binary search, greedy, two pointers Correct Solution: ``` from itertools import accumulate import sys def solve(f, g): n, m, k, s = [int(x) for x in f.readline().split()] a_price = [(int(x), i+1) for i, x in enumerate(f.readline().split())] b_price = [(int(x), i+1) for i, x in enumerate(f.readline().split())] a_gadgets = [] b_gadgets = [] for i, line in enumerate(f): t, price = [int(x) for x in line.split()] if t == 1: a_gadgets.append((price, i + 1)) else: b_gadgets.append((price, i + 1)) a_gadgets.sort() b_gadgets.sort() prefix_a = [0] + list(accumulate(gadget[0] for gadget in a_gadgets)) prefix_b = [0] + list(accumulate(gadget[0] for gadget in b_gadgets)) la = min(k, len(a_gadgets)) lb = min(k, len(b_gadgets)) min_price_for_k = [(prefix_a[i], prefix_b[k - i], i) for i in range(k-lb, la+1)] for i in range(1, n): a_price[i] = min(a_price[i], a_price[i-1]) b_price[i] = min(b_price[i], b_price[i-1]) def expence(day): return lambda x: a_price[day][0]*x[0] + b_price[day][0]*x[1] x, y = 0, n-1 while x <= y-1: day = (x + y) // 2 min_cost = min(min_price_for_k, key = expence(day)) if expence(day)(min_cost) > s: x = day+1 else: y = day min_cost = min(min_price_for_k, key = expence(x)) if expence(x)(min_cost) > s: g.write('-1\n') else: g.write(str(x+1) + '\n') i1 = min_cost[-1] A, B = ' ' + str(a_price[x][1]) + '\n', ' ' + str(b_price[x][1]) + '\n' for i in range(i1): g.write(str(a_gadgets[i][1]) + A) for i in range(k - i1): g.write(str(b_gadgets[i][1]) + B) solve(sys.stdin, sys.stdout) ```
output
1
92,159
10
184,319
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,160
10
184,320
Tags: binary search, greedy, two pointers Correct Solution: ``` from sys import stdin, stdout def ints(): return [int(x) for x in stdin.readline().split()] n, m, k, s = ints() a = ints() b = ints() d_gad = []; p_gad = []; for i in range(m): t, c = ints() if t == 1: d_gad.append([c, i + 1]) else: p_gad.append([c, i + 1]) d_gad.sort() p_gad.sort() mn_dol = [0] * n mn_pou = [0] * n day_mn_dol = [1] * n day_mn_pou = [1] * n mn_dol[0] = a[0] mn_pou[0] = b[0] for i in range(1, n): mn_dol[i] = min(mn_dol[i - 1], a[i]) day_mn_dol[i] = (i + 1 if mn_dol[i] == a[i] else day_mn_dol[i - 1]) for i in range(1, n): mn_pou[i] = min(mn_pou[i - 1], b[i]) day_mn_pou[i] = (i + 1 if mn_pou[i] == b[i] else day_mn_pou[i - 1]) def Check(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] SuM = 0 for u in range(k): if i == len(d_gad): SuM += p_gad[j][0] * mnp j += 1 elif j == len(p_gad): SuM += d_gad[i][0] * mnd i += 1 else: p1 = d_gad[i][0] * mnd p2 = p_gad[j][0] * mnp if p1 <= p2: SuM += p1 i += 1 else: SuM += p2 j += 1 if SuM > s: return False return True def Print_Ans(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] dayd = day_mn_dol[x] dayp = day_mn_pou[x] ans = [] for u in range(k): if i == len(d_gad): ans.append(str(p_gad[j][1]) + ' ' + str(dayp)) j += 1 elif j == len(p_gad): ans.append(str(d_gad[i][1]) + ' ' + str(dayd)) i += 1 else: if d_gad[i][0] * mnd <= p_gad[j][0] * mnp: ans.append(str(d_gad[i][1]) + ' ' + str(dayd)) i += 1 else: ans.append(str(p_gad[j][1]) + ' ' + str(dayp)) j += 1 stdout.write('\n'.join(ans)) if not Check(n - 1): print(-1) else: p = 0 q = n - 1 while p < q: mid = (p + q) // 2 if Check(mid): q = mid else: p = mid + 1 stdout.write(f'{p + 1}\n') Print_Ans(p) ```
output
1
92,160
10
184,321
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,161
10
184,322
Tags: binary search, greedy, two pointers Correct Solution: ``` import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' import heapq def can_do(check_days, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val): # get low in the range 1 -> check_days min_d = prefx_min_d_val[check_days][0] min_p = prefx_min_p_val[check_days][0] min_d_idx = prefx_min_d_val[check_days][1] min_p_idx = prefx_min_p_val[check_days][1] vals_d = [0] * (k + 1) vals_p = [0] * (k + 1) for i in range(1, k + 1): # print(i) if i <= len(d_gadgets): vals_d[i] = ((min_d * d_gadgets[i - 1][0]) + vals_d[i - 1]) if i <= len(p_gadgets): vals_p[i] = ((min_p * p_gadgets[i - 1][0]) + vals_p[i - 1]) for x in range(k + 1): # x from dollar and k - x from pounds if x <= len(d_gadgets) and k - x <= len(p_gadgets): if ((x > 0 and vals_d[x] > 0) or x == 0) \ and ((k - x > 0 and vals_p[k - x] > 0) or k - x == 0): total = vals_d[x] + vals_p[k - x] if total <= s: res = [] for i in range(x): res.append((d_gadgets[i][1], min_d_idx)) for i in range(k - x): res.append((p_gadgets[i][1], min_p_idx)) return (True, res) return (False, []) def check(n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val): lo = 0 hi = n res = -1 while lo <= hi: mid = lo + (hi - lo) // 2 if can_do(mid, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val)[0]: res = mid hi = mid - 1 else: lo = mid + 1 if res == -1: return res, [] return res, can_do(res, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val)[1] def solve(): n, m, k, s = map(int, input().split()) d_vals = list(map(int, input().split())) d_vals.insert(0, float("inf")) p_vals = list(map(int, input().split())) p_vals.insert(0, float("inf")) d_gadgets = [] p_gadgets = [] for i in range(m): t, c = map(int, input().split()) if t == 1: d_gadgets.append((c, i + 1)) else: p_gadgets.append((c, i + 1)) prefx_min_d_val = [(d_vals[0], 0)] * len(d_vals) for i in range(1, len(d_vals)): if prefx_min_d_val[i - 1][0] < d_vals[i]: prefx_min_d_val[i] = (prefx_min_d_val[i - 1][0], prefx_min_d_val[i - 1][1]) else: prefx_min_d_val[i] = (d_vals[i], i) prefx_min_p_val = [(p_vals[0], 0)] * len(p_vals) for i in range(1, len(p_vals)): if prefx_min_p_val[i - 1][0] < p_vals[i]: prefx_min_p_val[i] = (prefx_min_p_val[i - 1][0], prefx_min_p_val[i - 1][1]) else: prefx_min_p_val[i] = (p_vals[i], i) d_gadgets.sort(key=lambda x: x[0]) p_gadgets.sort(key=lambda x: x[0]) # print(prefx_min_d_val) # print(prefx_min_p_val) # print(d_gadgets) # print(p_gadgets) # can_do(9, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val) res, r = check(n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val) cout<<res<<endl for p in r: cout<<p[0]<<" "<<p[1]<<endl def main(): solve() if __name__ == "__main__": main() ```
output
1
92,161
10
184,323
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,162
10
184,324
Tags: binary search, greedy, two pointers Correct Solution: ``` import sys from itertools import accumulate n, m, k, s = map(int, input().split()) _rate = [list(map(int, input().split())), list(map(int, input().split()))] rate = [list(accumulate(_rate[0], min)), list(accumulate(_rate[1], min))] items = [[[0, -1]], [[0, -1]]] for i in range(m): t, c = map(int, sys.stdin.readline().split()) items[t-1].append([c, i+1]) items[0].sort() items[1].sort() for i in range(2): for j in range(1, len(items[i])): items[i][j][0] += items[i][j-1][0] ok, ng = n, -1 ans_size = [0, 0] while abs(ok - ng) > 1: mid = (ok + ng) >> 1 cnt_1 = min(len(items[0])-1, k) cnt_2 = k - cnt_1 while cnt_1 >= 0 and cnt_2 < len(items[1]): dollers = items[0][cnt_1][0] pounds = items[1][cnt_2][0] if dollers * rate[0][mid] + pounds * rate[1][mid] <= s: ok = mid ans_size = [cnt_1, cnt_2] break cnt_1 -= 1 cnt_2 += 1 else: ng = mid if ok < n: print(ok+1) for i in range(2): day = 0 for j in range(0, n): if _rate[i][j] == rate[i][ok]: day = j+1 break for j in range(1, ans_size[i]+1): print(items[i][j][1], day) else: print(-1) ```
output
1
92,162
10
184,325
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,163
10
184,326
Tags: binary search, greedy, two pointers Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=-10**9, func=lambda a, b: max(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ n,m,k,s=map(int,input().split()) w=list(map(int,input().split())) w1=list(map(int,input().split())) mi=[0]*n mi1=[0]*n mi[0]=w[0] mi1[0]=w1[0] d=[] p=[] for i in range(m): a,b=map(int,input().split()) if a==1: d.append((b,i)) else: p.append((b,i)) d.sort() p.sort() su=[0]*(len(d)+1) su1=[0]*(len(p)+1) for i in range(len(d)): su[i+1]=su[i]+d[i][0] for i in range(len(p)): su1[i+1]=su1[i]+p[i][0] for i in range(1,n): mi[i]=min(mi[i-1],w[i]) for i in range(1,n): mi1[i]=min(mi1[i-1],w1[i]) def find(x): x-=1 dol=mi[x] pou=mi1[x] for i in range(min(k,len(d))+1): if k-i>len(p): continue ans=dol*su[i] ans+=pou*su1[k-i] if ans<=s: return (True,i) return (False,-1) st=1 end=n ans=(-1,-1) while(st<=end): mid=(st+end)//2 r=find(mid) if r[0]==True: ans=[mid,r[1]] end=mid-1 else: st=mid+1 if ans[0]==-1: print(ans[0]) sys.exit(0) print(ans[0]) d1=w.index(min(w[:ans[0]])) p1=w1.index(min(w1[:ans[0]])) for i in range(ans[1]): print(d[i][1]+1,d1+1) for i in range(k-ans[1]): print(p[i][1]+1,p1+1) ```
output
1
92,163
10
184,327
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,164
10
184,328
Tags: binary search, greedy, two pointers Correct Solution: ``` ''' Auther: ghoshashis545 Ashis Ghosh College: Jalpaiguri Govt Enggineering College ''' from os import path from io import BytesIO, IOBase import sys from heapq import heappush,heappop from functools import cmp_to_key as ctk from collections import deque,Counter,defaultdict as dd from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right from itertools import permutations from datetime import datetime from math import ceil,sqrt,log,gcd def ii():return int(input()) def si():return input().rstrip() def mi():return map(int,input().split()) def li():return list(mi()) abc='abcdefghijklmnopqrstuvwxyz' mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def bo(i): return ord(i)-ord('0') file = 1 def ceil(a,b): return (a+b-1)//b def solve(): # for _ in range(1,ii()+1): n,m,k,s = mi() a = li() b = li() gadgets = [[] for i in range(2)] for i in range(m): x,y = mi() gadgets[x-1].append([y,i+1]) gadgets[0].sort() gadgets[1].sort() sz = [len(gadgets[0]),len(gadgets[1])] def check(idx): mnx1 = inf mnx2 = inf for i in range(idx+1): mnx1 = min(mnx1,a[i]) mnx2 = min(mnx2,b[i]) l1,l2,res = 0,0,0 if k > sz[0] + sz[1]: return 0 for i in range(k): if l1 == sz[0]: res += gadgets[1][l2][0]*mnx2 l2 += 1 elif l2 == sz[1]: res += gadgets[0][l1][0]*mnx1 l1 += 1 elif gadgets[0][l1][0]*mnx1 <= gadgets[1][l2][0]*mnx2: res += gadgets[0][l1][0]*mnx1 l1 += 1 else: res += gadgets[1][l2][0]*mnx2 l2 += 1 return res <= s l = 0 r = n-1 ans = -1 while l<=r: mid = (l+r)>>1 if check(mid): ans = mid+1 r = mid-1 else: l=mid+1 print(ans) if ans == -1: return mnx1 = inf mnx2 = inf idx1,idx2 = -1,-1 for i in range(ans): if a[i] < mnx1: mnx1 = a[i] idx1 = i if b[i] < mnx2: mnx2 = b[i] idx2 = i l1,l2,res = 0,0,[] for i in range(k): if l1 == sz[0]: res.append([gadgets[1][l2][1],idx2+1]) l2 += 1 elif l2==sz[1]: res.append([gadgets[0][l1][1],idx1+1]) l1 += 1 elif gadgets[0][l1][0]*mnx1 <= gadgets[1][l2][0]*mnx2: res.append([gadgets[0][l1][1],idx1+1]) l1 += 1 else: res.append([gadgets[1][l2][1],idx2+1]) l2 += 1 for i in res: print(*i) if __name__ =="__main__": if(file): if path.exists('input.txt'): sys.stdin=open('input.txt', 'r') sys.stdout=open('output.txt','w') else: input=sys.stdin.readline solve() ```
output
1
92,164
10
184,329
Provide tags and a correct Python 3 solution for this coding contest problem. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1
instruction
0
92,165
10
184,330
Tags: binary search, greedy, two pointers Correct Solution: ``` from sys import stdin, stdout def ints(): return [int(x) for x in stdin.readline().split()] n, m, k, s = ints() a = ints() b = ints() d_gad = []; p_gad = []; for i in range(m): t, c = ints() if t == 1: d_gad.append([c, i + 1]) else: p_gad.append([c, i + 1]) d_gad.sort() p_gad.sort() mn_dol = [0] * n mn_pou = [0] * n day_mn_dol = [1] * n day_mn_pou = [1] * n mn_dol[0] = a[0] mn_pou[0] = b[0] for i in range(1, n): mn_dol[i] = min(mn_dol[i - 1], a[i]) day_mn_dol[i] = (i + 1 if mn_dol[i] == a[i] else day_mn_dol[i - 1]) for i in range(1, n): mn_pou[i] = min(mn_pou[i - 1], b[i]) day_mn_pou[i] = (i + 1 if mn_pou[i] == b[i] else day_mn_pou[i - 1]) def Check(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] SuM = 0 for u in range(k): if i == len(d_gad): SuM += p_gad[j][0] * mnp j += 1 elif j == len(p_gad): SuM += d_gad[i][0] * mnd i += 1 else: p1 = d_gad[i][0] * mnd p2 = p_gad[j][0] * mnp if p1 <= p2: SuM += p1 i += 1 else: SuM += p2 j += 1 if SuM > s: return False return True def Print_Ans(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] dayd = day_mn_dol[x] dayp = day_mn_pou[x] for u in range(k): if i == len(d_gad): stdout.write(f'{p_gad[j][1]} {dayp}\n') j += 1 elif j == len(p_gad): stdout.write(f'{d_gad[i][1]} {dayd}\n') i += 1 else: if d_gad[i][0] * mnd <= p_gad[j][0] * mnp: stdout.write(f'{d_gad[i][1]} {dayd}\n') i += 1 else: stdout.write(f'{p_gad[j][1]} {dayp}\n') j += 1 if not Check(n - 1): print(-1) else: p = 0 q = n - 1 while p < q: mid = (p + q) // 2 if Check(mid): q = mid else: p = mid + 1 stdout.write(f'{p + 1}\n') Print_Ans(p) ```
output
1
92,165
10
184,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1 Submitted Solution: ``` from sys import stdin, stdout def ints(): return [int(x) for x in stdin.readline().split()] n, m, k, s = ints() a = ints() b = ints() d_gad = [(0,)] * m; p_gad = [(0,)] * m; ix = 0 jx = 0 for i in range(m): t, c = ints() if t == 1: d_gad[ix] = (c, i + 1) ix += 1 else: p_gad[jx] = (c, i + 1) jx += 1 d_gad = sorted(d_gad[:ix]) p_gad = sorted(p_gad[:jx]) mn_dol = [0] * n mn_pou = [0] * n day_mn_dol = [1] * n day_mn_pou = [1] * n mn_dol[0] = a[0] mn_pou[0] = b[0] for i in range(1, n): mn_dol[i] = min(mn_dol[i - 1], a[i]) day_mn_dol[i] = (i + 1 if mn_dol[i] == a[i] else day_mn_dol[i - 1]) for i in range(1, n): mn_pou[i] = min(mn_pou[i - 1], b[i]) day_mn_pou[i] = (i + 1 if mn_pou[i] == b[i] else day_mn_pou[i - 1]) def Check(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] SuM = 0 for u in range(k): if i == ix: SuM += p_gad[j][0] * mnp j += 1 elif j == jx: SuM += d_gad[i][0] * mnd i += 1 else: p1 = d_gad[i][0] * mnd p2 = p_gad[j][0] * mnp if p1 <= p2: SuM += p1 i += 1 else: SuM += p2 j += 1 if SuM > s: return False return True def Print_Ans(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] dayd = day_mn_dol[x] dayp = day_mn_pou[x] for u in range(k): if i == ix: stdout.write(f'{p_gad[j][1]} {dayp}\n') j += 1 elif j == jx: stdout.write(f'{d_gad[i][1]} {dayd}\n') i += 1 else: if d_gad[i][0] * mnd <= p_gad[j][0] * mnp: stdout.write(f'{d_gad[i][1]} {dayd}\n') i += 1 else: stdout.write(f'{p_gad[j][1]} {dayp}\n') j += 1 if not Check(n - 1): stdout.write('-1\n') else: p = 0 q = n - 1 while p < q: mid = (p + q) // 2 if Check(mid): q = mid else: p = mid + 1 stdout.write(f'{p + 1}\n') Print_Ans(p) ```
instruction
0
92,166
10
184,332
Yes
output
1
92,166
10
184,333
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1 Submitted Solution: ``` from itertools import accumulate import sys def solve(f): n, m, k, s = [int(x) for x in f.readline().split()] a_price = [(int(x), i+1) for i, x in enumerate(f.readline().split())] b_price = [(int(x), i+1) for i, x in enumerate(f.readline().split())] a_gadgets = [] b_gadgets = [] for i, line in enumerate(f): t, price = [int(x) for x in line.split()] if t == 1: a_gadgets.append((price, i + 1)) else: b_gadgets.append((price, i + 1)) a_gadgets.sort() b_gadgets.sort() prefix_a = [0] + list(accumulate(gadget[0] for gadget in a_gadgets)) prefix_b = [0] + list(accumulate(gadget[0] for gadget in b_gadgets)) la = min(k, len(a_gadgets)) lb = min(k, len(b_gadgets)) min_price_for_k = [(prefix_a[i], prefix_b[k - i], i) for i in range(k-lb, la+1)] min_a, min_b = [a_price[0]], [b_price[0]] for i in range(1, n): min_a.append(min(min_a[i-1], a_price[i])) min_b.append(min(min_b[i-1], b_price[i])) def expence(day): return lambda x: min_a[day][0]*x[0] + min_b[day][0]*x[1] x, y = 0, n-1 while x <= y-1: day = (x + y) // 2 min_cost = min(min_price_for_k, key = expence(day)) if expence(day)(min_cost) > s: x = day+1 else: y = day min_cost = min(min_price_for_k, key = expence(x)) if expence(x)(min_cost) > s: print(-1) else: print(x+1) i1 = min_cost[-1] for i in range(i1): print(a_gadgets[i][1], min_a[x][1]) for i in range(k - i1): print(b_gadgets[i][1], min_b[x][1]) from time import time t = time() solve(sys.stdin) print(time() - t) ```
instruction
0
92,167
10
184,334
No
output
1
92,167
10
184,335
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1 Submitted Solution: ``` from sys import stdin, stdout def ints(): return [int(x) for x in stdin.readline().split()] n, m, k, s = ints() a = ints() b = ints() d_gad = [(0,)] * m; p_gad = [(0,)] * m; ix = 0 jx = 0 for i in range(m): t, c = ints() if t == 1: d_gad[ix] = (c, i + 1) ix += 1 else: p_gad[jx] = (c, i + 1) jx += 1 d_gad[:ix].sort() p_gad[:jx].sort() mn_dol = [0] * n mn_pou = [0] * n day_mn_dol = [1] * n day_mn_pou = [1] * n mn_dol[0] = a[0] mn_pou[0] = b[0] for i in range(1, n): mn_dol[i] = min(mn_dol[i - 1], a[i]) day_mn_dol[i] = (i + 1 if mn_dol[i] == a[i] else day_mn_dol[i - 1]) for i in range(1, n): mn_pou[i] = min(mn_pou[i - 1], b[i]) day_mn_pou[i] = (i + 1 if mn_pou[i] == b[i] else day_mn_pou[i - 1]) def Check(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] SuM = 0 for u in range(k): if i == ix: SuM += p_gad[j][0] * mnp j += 1 elif j == jx: SuM += d_gad[i][0] * mnd i += 1 else: p1 = d_gad[i][0] * mnd p2 = p_gad[j][0] * mnp if p1 <= p2: SuM += p1 i += 1 else: SuM += p2 j += 1 if SuM > s: return False return True def Print_Ans(x): i = 0 j = 0 mnd = mn_dol[x] mnp = mn_pou[x] dayd = day_mn_dol[x] dayp = day_mn_pou[x] for u in range(k): if i == ix: stdout.write(f'{p_gad[j][1]} {dayp}\n') j += 1 elif j == jx: stdout.write(f'{d_gad[i][1]} {dayd}\n') i += 1 else: if d_gad[i][0] * mnd <= p_gad[j][0] * mnp: stdout.write(f'{d_gad[i][1]} {dayd}\n') i += 1 else: stdout.write(f'{p_gad[j][1]} {dayp}\n') j += 1 if not Check(n - 1): stdout.write('-1\n') else: p = 0 q = n - 1 while p < q: mid = (p + q) // 2 if Check(mid): q = mid else: p = mid + 1 stdout.write(f'{p + 1}\n') Print_Ans(p) ```
instruction
0
92,168
10
184,336
No
output
1
92,168
10
184,337
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1 Submitted Solution: ``` def check(x): global n, m, k, s, q, c, d cur1 = 0 cur2 = 0 q.clear() for i in range(x): if a[i] < a[cur1]: cur1 = i if b[i] < b[cur2]: cur2 = i for i in range(k): if c[i] == 1: q.append((d[i] * a[cur1], i, cur1)) else: q.append((d[i] * b[cur2], i, cur2)) q.sort() cur = 0 for i in range(k): cur += q[i][0] return cur <= s n, m, k, s = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] d = [] for i in range(m): x, y = map(int, input().split()) c.append(x) d.append(y) l = 1 r = n q = [] while r - l > 1: mid = (r + l) >> 1 if check(mid): r = mid else: l = mid if check(l): print(l) for i in range(k): print(q[i][1] + 1, q[i][2] + 1) else: if check(r): print(r) for i in range(k): print(q[i][1] + 1, q[i][2] + 1) else: print(-1) ```
instruction
0
92,169
10
184,338
No
output
1
92,169
10
184,339
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days. Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input. Input First line contains four integers n, m, k, s (1 ≀ n ≀ 2Β·105, 1 ≀ k ≀ m ≀ 2Β·105, 1 ≀ s ≀ 109) β€” number of days, total number and required number of gadgets, number of burles Nura has. Second line contains n integers ai (1 ≀ ai ≀ 106) β€” the cost of one dollar in burles on i-th day. Third line contains n integers bi (1 ≀ bi ≀ 106) β€” the cost of one pound in burles on i-th day. Each of the next m lines contains two integers ti, ci (1 ≀ ti ≀ 2, 1 ≀ ci ≀ 106) β€” type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy k gadgets print the only line with the number -1. Otherwise the first line should contain integer d β€” the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers qi, di β€” the number of gadget and the day gadget should be bought. All values qi should be different, but the values di can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Output 3 1 1 2 3 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Output -1 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432 Output -1 Submitted Solution: ``` import os, sys from io import IOBase, BytesIO py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = 'x' in file.mode or 'w' in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b'\n') + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: 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') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' import heapq def can_do(check_days, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val): # get low in the range 1 -> check_days min_d = prefx_min_d_val[check_days][0] min_p = prefx_min_p_val[check_days][0] min_d_idx = prefx_min_d_val[check_days][1] min_p_idx = prefx_min_p_val[check_days][1] vals_d = [0] * (k + 1) vals_p = [0] * (k + 1) for i in range(1, k + 1): # print(i) if i < len(d_gadgets): vals_d[i] = ((min_d * d_gadgets[i - 1][0]) + vals_d[i - 1]) if i < len(p_gadgets): vals_p[i] = ((min_p * p_gadgets[i - 1][0]) + vals_p[i - 1]) for x in range(k + 1): # x from dollar and k - x from pounds if x <= len(d_gadgets) and k - x <= len(p_gadgets): if ((x > 0 and vals_d[x] > 0) or x == 0) \ and ((k - x > 0 and vals_p[k - x] > 0) or k - x == 0): total = vals_d[x] + vals_p[k - x] if total <= s: res = [] for i in range(x): res.append((d_gadgets[i][1], min_d_idx)) for i in range(k - x): res.append((p_gadgets[i][1], min_p_idx)) return (True, res) return (False, []) def check(n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val): lo = 0 hi = n res = -1 while lo <= hi: mid = lo + (hi - lo) // 2 if can_do(mid, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val)[0]: res = mid hi = mid - 1 else: lo = mid + 1 if res == -1: return res, [] return res, can_do(res, n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val)[1] def solve(): n, m, k, s = map(int, input().split()) d_vals = list(map(int, input().split())) d_vals.insert(0, float("inf")) p_vals = list(map(int, input().split())) p_vals.insert(0, float("inf")) d_gadgets = [] p_gadgets = [] for i in range(m): t, c = map(int, input().split()) if t == 1: d_gadgets.append((c, i + 1)) else: p_gadgets.append((c, i + 1)) prefx_min_d_val = [(d_vals[0], 0)] * len(d_vals) for i in range(1, len(d_vals)): if prefx_min_d_val[i - 1][0] < d_vals[i]: prefx_min_d_val[i] = (prefx_min_d_val[i - 1][0], prefx_min_d_val[i - 1][1]) else: prefx_min_d_val[i] = (d_vals[i], i) prefx_min_p_val = [(p_vals[0], 0)] * len(p_vals) for i in range(1, len(p_vals)): if prefx_min_p_val[i - 1][0] < p_vals[i]: prefx_min_p_val[i] = (prefx_min_p_val[i - 1][0], prefx_min_p_val[i - 1][1]) else: prefx_min_p_val[i] = (p_vals[i], i) d_gadgets.sort(key=lambda x: x[0]) p_gadgets.sort(key=lambda x: x[0]) res, r = check(n, m, k, s, d_gadgets, p_gadgets, prefx_min_d_val, prefx_min_p_val) cout<<res<<endl for p in r: cout<<p[0]<<" "<<p[1]<<endl def main(): solve() if __name__ == "__main__": main() ```
instruction
0
92,170
10
184,340
No
output
1
92,170
10
184,341
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,283
10
184,566
Tags: brute force, implementation Correct Solution: ``` class CodeforcesTask794ASolution: def __init__(self): self.result = '' self.a_b_c = [] self.n = 0 self.banknotes = [] def read_input(self): self.a_b_c = [int(x) for x in input().split(" ")] self.n = int(input()) self.banknotes = [int(x) for x in input().split(" ")] def process_task(self): notes = 0 for note in self.banknotes: if self.a_b_c[1] < note < self.a_b_c[2]: notes += 1 self.result = str(notes) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask794ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result()) ```
output
1
92,283
10
184,567
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,284
10
184,568
Tags: brute force, implementation Correct Solution: ``` a, b, c = map(int, input().split()) n = int(input()) l = list(map(int, input().split())) ans = 0 for i in l: if i in range(b+1, c): ans += 1 print(ans) ```
output
1
92,284
10
184,569
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,285
10
184,570
Tags: brute force, implementation Correct Solution: ``` a,b,c = list(map(int,input().split())) n = int(input()) a = list(map(int,input().split())) ans = 0 for i in a: if b < i < c: ans += 1 print(ans) ```
output
1
92,285
10
184,571
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,286
10
184,572
Tags: brute force, implementation Correct Solution: ``` def main_function(): a, b, c = [int(i) for i in input().split(" ")] n = int(input()) x = [int(i) for i in input().split(" ")] banknotes = 0 for i in x: if i > b and i < c: banknotes += 1 return str(banknotes) print(main_function()) ```
output
1
92,286
10
184,573
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,287
10
184,574
Tags: brute force, implementation Correct Solution: ``` #----Kuzlyaev-Nikita-Codeforces----- #------------03.04.2020------------- alph="abcdefghijklmnopqrstuvwxyz" #----------------------------------- a,b,c=map(int,input().split()) n=int(input()) x=list(map(int,input().split())) E=0 for i in range(n): if x[i]>b and x[i]<c: E+=1 print(E) ```
output
1
92,287
10
184,575
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,288
10
184,576
Tags: brute force, implementation Correct Solution: ``` a,b,c=map(int,input().split()) d=int(input()) l=list(map(int,input().split())) i=0 e=0 while(i<d): if l[i]>b and l[i]<c: e+=1 i+=1 print(e) ```
output
1
92,288
10
184,577
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,289
10
184,578
Tags: brute force, implementation Correct Solution: ``` l=list(map(int,input().rstrip().split())) n=int(input()) l1=list(map(int,input().rstrip().split())) c=0 for i in l1: if (i>l[1] and i<l[2]): c+=1 print(c) ```
output
1
92,289
10
184,579
Provide tags and a correct Python 3 solution for this coding contest problem. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards.
instruction
0
92,290
10
184,580
Tags: brute force, implementation Correct Solution: ``` (n,a,b) = [int(x) for x in input().split()] number = int(input()) notes = [int(x) for x in input().split()] answer = 0 for i in range(len(notes)): if a<notes[i]<b: answer+=1 print(answer) ```
output
1
92,290
10
184,581
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` T_ON = 0 DEBUG_ON = 1 MOD = 998244353 def solve(): c, a, b = read_ints() n = read_int() A = read_ints() count = 0 for x in A: if a < x < b: count += 1 print(count) def main(): T = read_int() if T_ON else 1 for i in range(T): solve() def debug(*xargs): if DEBUG_ON: print(*xargs) from collections import * import math #---------------------------------FAST_IO--------------------------------------- import os import sys from io import BytesIO, IOBase # 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") #----------------------------------IO_WRAP-------------------------------------- def read_int(): return int(input()) def read_ints(): return list(map(int, input().split())) def print_nums(nums): print(" ".join(map(str, nums))) def YES(): print("YES") def Yes(): print("Yes") def NO(): print("NO") def No(): print("No") def First(): print("First") def Second(): print("Second") #----------------------------------FIB-------------------------------------- def fib(n): """ the nth fib, start from zero """ a, b = 0, 1 for _ in range(n): a, b = b, a + b return a def fib_ns(n): """ the first n fibs, start from zero """ assert n >= 1 f = [0 for _ in range(n + 1)] f[0] = 0 f[1] = 1 for i in range(2, n + 1): f[i] = f[i - 1] + f[i - 2] return f def fib_to_n(n): """ return fibs <= n, start from zero n=8 f=[0,1,1,2,3,5,8] """ f = [] a, b = 0, 1 while a <= n: f.append(a) a, b = b, a + b return f #----------------------------------MOD-------------------------------------- def gcd(a, b): if a == 0: return b return gcd(b % a, a) def xgcd(a, b): """return (g, x, y) such that a*x + b*y = g = gcd(a, b)""" x0, x1, y0, y1 = 0, 1, 1, 0 while a != 0: (q, a), b = divmod(b, a), a y0, y1 = y1, y0 - q * y1 x0, x1 = x1, x0 - q * x1 return b, x0, y0 def lcm(a, b): d = gcd(a, b) return a * b // d def is_even(x): return x % 2 == 0 def is_odd(x): return x % 2 == 1 def modinv(a, m): """return x such that (a * x) % m == 1""" g, x, _ = xgcd(a, m) if g != 1: raise Exception('gcd(a, m) != 1') return x % m def mod_add(x, y): x += y while x >= MOD: x -= MOD while x < 0: x += MOD return x def mod_mul(x, y): return (x * y) % MOD def mod_pow(x, y): if y == 0: return 1 if y % 2: return mod_mul(x, mod_pow(x, y - 1)) p = mod_pow(x, y // 2) return mod_mul(p, p) def mod_inv(y): return mod_pow(y, MOD - 2) def mod_div(x, y): # y^(-1): Fermat little theorem, MOD is a prime return mod_mul(x, mod_inv(y)) #---------------------------------PRIME--------------------------------------- def is_prime(n): if n == 1: return False for i in range(2, int(n ** 0.5) + 1): if n % i: return False return True def gen_primes(n): """ generate primes of [1..n] using sieve's method """ P = [True for _ in range(n + 1)] P[0] = P[1] = False for i in range(int(n ** 0.5) + 1): if P[i]: for j in range(2 * i, n + 1, i): P[j] = False return P #---------------------------------MISC--------------------------------------- def is_lucky(n): return set(list(str(n))).issubset({'4', '7'}) #---------------------------------MAIN--------------------------------------- main() ```
instruction
0
92,291
10
184,582
Yes
output
1
92,291
10
184,583
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` n,m,o = map(int,input().split()) a = int(input()) b = sorted(list(map(int,input().split()))) z =0 for i in b: if i>m and i<o: z+=1 print(z) ```
instruction
0
92,292
10
184,584
Yes
output
1
92,292
10
184,585
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` #!/usr/bin/env python3 from sys import stdin, stdout def rint(): return map(int, stdin.readline().split()) #lines = stdin.readlines() a, b, c = rint() n = int(input()) x = list(rint()) ans = 0 for i in x: if i > b and i < c: ans += 1 print(ans) ```
instruction
0
92,293
10
184,586
Yes
output
1
92,293
10
184,587
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` a, b, c = map(int, input().split()) n = int(input()) x = map(int, input().split()) ans = 0 for i in x: if b < i < c: ans += 1 print(ans) ```
instruction
0
92,294
10
184,588
Yes
output
1
92,294
10
184,589
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` from bisect import bisect_left def num_notes(x, left, right): count = 0 for i in range(left, right + 1): count += x[i] return count def search_left(a, x, lo=0, hi=None): hi = hi if hi is not None else len(a) pos = bisect_left(a, x, lo, hi) return (pos if pos != hi and a[pos] == x else pos + 1) def search_right(a, x, lo=0, hi=None): hi = hi if hi is not None else len(a) pos = bisect_left(a, x, lo, hi) return (pos if pos != hi and a[pos] == x else pos - 1) if __name__ == '__main__': a, b, c = [int(num) for num in input().split()] n = int(input()) x = [] for i in range(0, n): x.append(int(n)) x.sort() left = search_left(x, a) right = search_right(x, b) print(num_notes(x, left, right)) ```
instruction
0
92,295
10
184,590
No
output
1
92,295
10
184,591
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` ###################################################### def convert_inp_to_int(): return [int(x) for x in input().strip().split()] ####################################################### def get_position(flag,n,notes_at,pos): if(flag): i=0 while(i<n): if(notes_at[i]>pos): return (i-1) i+=1 return (i-1) else: i=0 while(i<n): if(notes_at[i]>=pos): return (i-1) i+=1 return (i-1) #################################################### abc=convert_inp_to_int() a=abc[0] b=abc[1] c=abc[2] n=convert_inp_to_int()[0] notes_at=convert_inp_to_int() notes_at.sort() #print(notes_at) g1_pos=get_position(1,n,notes_at,b) g2_pos=get_position(0,n,notes_at,c) g1_pos+=1 #print(oleg_pos,g1_pos,g2_pos) if(g1_pos<g2_pos): print(g2_pos-g1_pos+1) else: print(0) ```
instruction
0
92,296
10
184,592
No
output
1
92,296
10
184,593
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` l=input().split(" ") n=int(input()) k=input().split(" ") b=[] s=0 for i in range(int(l[1])+1,int(l[2])): b.append(i) for j in k: if(int(j) in b): s=s+k.count(j) for i in range(k.count(j)): k.remove(j) print(s) ```
instruction
0
92,297
10
184,594
No
output
1
92,297
10
184,595
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the i-th safe from the left is called safe i. There are n banknotes left in all the safes in total. The i-th banknote is in safe xi. Oleg is now at safe a. There are two security guards, one of which guards the safe b such that b < a, i.e. the first guard is to the left of Oleg. The other guard guards the safe c so that c > a, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, a, b and c (1 ≀ b < a < c ≀ 109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer n (1 ≀ n ≀ 105), denoting the number of banknotes. The next line of input contains n space-separated integers x1, x2, ..., xn (1 ≀ xi ≀ 109), denoting that the i-th banknote is located in the xi-th safe. Note that xi are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Output 4 Input 6 5 7 5 1 5 7 92 3 Output 0 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. Submitted Solution: ``` a,b,c = input().split(' ') a,b,c = int(a),int(b),int(c) input() safes = range(b+1,c) line3 = input().split(' ') c = 0 for sf in line3: if sf in safes: c+= 1 print(c) ```
instruction
0
92,298
10
184,596
No
output
1
92,298
10
184,597
Provide tags and a correct Python 3 solution for this coding contest problem. A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. Input The first line contains a single integer n β€” the number of companies in the conglomerate (1 ≀ n ≀ 2 β‹… 10^5). Each of the next n lines describes a company. A company description start with an integer m_i β€” the number of its employees (1 ≀ m_i ≀ 2 β‹… 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9. The total number of employees in all companies does not exceed 2 β‹… 10^5. Output Output a single integer β€” the minimal total increase of all employees that allows to merge all companies. Example Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 Note One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
instruction
0
92,645
10
185,290
Tags: greedy Correct Solution: ``` def func(inpstr): return inpstr[0] N = int(input()) arr = [] money = 0 for i in range(N): a = [int(i) for i in input().split()] arr.append([max(a[1::])] + [a[0]]) arr.sort(key = func) while(len(arr) != 1): p = arr[-2][1] m = arr[-1][0] - arr[-2][0] money += m * p arr[-1][1] += p arr.pop(-2) print(money) ```
output
1
92,645
10
185,291
Provide tags and a correct Python 3 solution for this coding contest problem. A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. Input The first line contains a single integer n β€” the number of companies in the conglomerate (1 ≀ n ≀ 2 β‹… 10^5). Each of the next n lines describes a company. A company description start with an integer m_i β€” the number of its employees (1 ≀ m_i ≀ 2 β‹… 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9. The total number of employees in all companies does not exceed 2 β‹… 10^5. Output Output a single integer β€” the minimal total increase of all employees that allows to merge all companies. Example Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 Note One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
instruction
0
92,646
10
185,292
Tags: greedy Correct Solution: ``` n=int(input()) a=[] b=[] for i in range (n): s=list (map (int, input().strip().split())) a.append(s.pop(0)) b.append(max(s)) c=max(b) sum = 0 for i in range(n): sum+=(c-b[i])*a[i] print(sum) ```
output
1
92,646
10
185,293
Provide tags and a correct Python 3 solution for this coding contest problem. A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. Input The first line contains a single integer n β€” the number of companies in the conglomerate (1 ≀ n ≀ 2 β‹… 10^5). Each of the next n lines describes a company. A company description start with an integer m_i β€” the number of its employees (1 ≀ m_i ≀ 2 β‹… 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9. The total number of employees in all companies does not exceed 2 β‹… 10^5. Output Output a single integer β€” the minimal total increase of all employees that allows to merge all companies. Example Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 Note One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
instruction
0
92,647
10
185,294
Tags: greedy Correct Solution: ``` n = int(input()) CompS = [] S = 0 for i in range(n): m, *H = list(map(int, input().split())) Max = max(H) CompS.append((Max, m)) CompS.sort(reverse=True) for i in range(1, len(CompS)): S += (CompS[0][0] - CompS[i][0]) * CompS[i][1] print(S) ```
output
1
92,647
10
185,295
Provide tags and a correct Python 3 solution for this coding contest problem. A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. Input The first line contains a single integer n β€” the number of companies in the conglomerate (1 ≀ n ≀ 2 β‹… 10^5). Each of the next n lines describes a company. A company description start with an integer m_i β€” the number of its employees (1 ≀ m_i ≀ 2 β‹… 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9. The total number of employees in all companies does not exceed 2 β‹… 10^5. Output Output a single integer β€” the minimal total increase of all employees that allows to merge all companies. Example Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 Note One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
instruction
0
92,648
10
185,296
Tags: greedy Correct Solution: ``` n = int(input()) b = [int(x) for x in input().split()] q = b[0] p = max(b[1:]) total = 0 for i in range(n-1): a = [int(x) for x in input().split()] c = a[0] m = max(a[1:]) if p > m: total += c * (p - m) elif m > p: total += q * (m - p) p = m q += c print(total) ```
output
1
92,648
10
185,297
Provide tags and a correct Python 3 solution for this coding contest problem. A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. Input The first line contains a single integer n β€” the number of companies in the conglomerate (1 ≀ n ≀ 2 β‹… 10^5). Each of the next n lines describes a company. A company description start with an integer m_i β€” the number of its employees (1 ≀ m_i ≀ 2 β‹… 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9. The total number of employees in all companies does not exceed 2 β‹… 10^5. Output Output a single integer β€” the minimal total increase of all employees that allows to merge all companies. Example Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 Note One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
instruction
0
92,649
10
185,298
Tags: greedy Correct Solution: ``` from sys import stdin, stdout input = stdin.readline n = (int)(input()) a,s,t =[],0,[] for i in range(n): b = list(map(int,input().split())) a.append(max(b[1:])) t.append(b[0]) boro = max(a) for i in range(n): s+=(boro-a[i])*t[i] stdout.write(str(s)) ```
output
1
92,649
10
185,299
Provide tags and a correct Python 3 solution for this coding contest problem. A conglomerate consists of n companies. To make managing easier, their owners have decided to merge all companies into one. By law, it is only possible to merge two companies, so the owners plan to select two companies, merge them into one, and continue doing so until there is only one company left. But anti-monopoly service forbids to merge companies if they suspect unfriendly absorption. The criterion they use is the difference in maximum salaries between two companies. Merging is allowed only if the maximum salaries are equal. To fulfill the anti-monopoly requirements, the owners can change salaries in their companies before merging. But the labor union insists on two conditions: it is only allowed to increase salaries, moreover all the employees in one company must get the same increase. Sure enough, the owners want to minimize the total increase of all salaries in all companies. Help them find the minimal possible increase that will allow them to merge companies into one. Input The first line contains a single integer n β€” the number of companies in the conglomerate (1 ≀ n ≀ 2 β‹… 10^5). Each of the next n lines describes a company. A company description start with an integer m_i β€” the number of its employees (1 ≀ m_i ≀ 2 β‹… 10^5). Then m_i integers follow: the salaries of the employees. All salaries are positive and do not exceed 10^9. The total number of employees in all companies does not exceed 2 β‹… 10^5. Output Output a single integer β€” the minimal total increase of all employees that allows to merge all companies. Example Input 3 2 4 3 2 2 1 3 1 1 1 Output 13 Note One of the optimal merging strategies is the following. First increase all salaries in the second company by 2, and merge the first and the second companies. Now the conglomerate consists of two companies with salaries [4, 3, 4, 3] and [1, 1, 1]. To merge them, increase the salaries in the second of those by 3. The total increase is 2 + 2 + 3 + 3 + 3 = 13.
instruction
0
92,650
10
185,300
Tags: greedy Correct Solution: ``` import sys input=sys.stdin.readline N=int(input()) tot=[] for i in range(N): x=list(map(int,input().split())) maxim=max(x[1:]) n=x[0] tot.append((maxim,n)) tot.sort() counter=0 summa=tot[0][1] for i in range(1,len(tot)): counter+=(tot[i][0]-tot[i-1][0])*summa summa+=tot[i][1] print(counter) ```
output
1
92,650
10
185,301