message
stringlengths
2
43.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
853
107k
cluster
float64
24
24
__index_level_0__
int64
1.71k
214k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work. Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100, 1 ≀ m ≀ 10^4) β€” the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days in this test. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` n,m = map(int,input().split()) arr = list(map(int,input().split())) arr = sorted(arr,reverse = True) #print(arr) curr,sumi,flag = 0,0,0 for i in range(n): sumi = curr + arr[i] if sumi >= m: print(i) break curr = sumi for j in range(i+1,n): if arr[j]-j-i > 0: sumi += arr[j]-j-i else: break if sumi >= m: flag = 1 break if flag == 1: print(i+1) break #print(i+1,sumi,curr) ```
instruction
0
82,642
24
165,284
No
output
1
82,642
24
165,285
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work. Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100, 1 ≀ m ≀ 10^4) β€” the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days in this test. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` n,m=map(int,input().split()) cc=sum(map(int,input().split())) if cc<m: print(-1) else: r=0 rv=0 while cc > m: a=int((-1 + (1+8*(cc-m))**0.5)/2) cc-=a*(a+1)/2 r+=1 rv+=(1+a) print(min(max(n-rv+1, r),n)) ```
instruction
0
82,643
24
165,286
No
output
1
82,643
24
165,287
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of m pages. Polycarp also has n cups of coffee. The coffee in the i-th cup has a_i caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work. Let's consider some day of Polycarp's work. Consider Polycarp drinks k cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are a_{i_1}, a_{i_2}, ..., a_{i_k}. Then the first cup he drinks gives him energy to write a_{i_1} pages of coursework, the second cup gives him energy to write max(0, a_{i_2} - 1) pages, the third cup gives him energy to write max(0, a_{i_3} - 2) pages, ..., the k-th cup gives him energy to write max(0, a_{i_k} - k + 1) pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers n and m (1 ≀ n ≀ 100, 1 ≀ m ≀ 10^4) β€” the number of cups of coffee and the number of pages in the coursework. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 100), where a_i is the caffeine dosage of coffee in the i-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Examples Input 5 8 2 3 1 1 2 Output 4 Input 7 10 1 3 4 2 1 4 2 Output 2 Input 5 15 5 5 5 5 5 Output 1 Input 5 16 5 5 5 5 5 Output 2 Input 5 26 5 5 5 5 5 Output -1 Note In the first example Polycarp can drink fourth cup during first day (and write 1 page), first and second cups during second day (and write 2 + (3 - 1) = 4 pages), fifth cup during the third day (and write 2 pages) and third cup during the fourth day (and write 1 page) so the answer is 4. It is obvious that there is no way to write the coursework in three or less days in this test. In the second example Polycarp can drink third, fourth and second cups during first day (and write 4 + (2 - 1) + (3 - 2) = 6 pages) and sixth cup during second day (and write 4 pages) so the answer is 2. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write 5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15 pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write 5 + (5 - 1) + (5 - 2) + (5 - 3) = 14 pages of coursework and during second day he will write 5 pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1. Submitted Solution: ``` n,m=list(map(int,input().split())) L=list(map(int,input().split())) L.sort(reverse=True) for k in range(1,n): sm=0 for i in range(n-1): sm += max(L[i] - i//k, 0) if sm>=m: print(k) exit(0) else: print("-1") ```
instruction
0
82,644
24
165,288
No
output
1
82,644
24
165,289
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,424
24
166,848
Tags: data structures, greedy, sortings Correct Solution: ``` n = int(input()) ent = list(map(int,input().split()))[:n] ent.sort() res = 0 err = 0 index_trav = 0 for i in range(0, n): if ent[res + err] > index_trav: res += 1 index_trav += 1 else: err += 1 print(res) ```
output
1
83,424
24
166,849
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,425
24
166,850
Tags: data structures, greedy, sortings Correct Solution: ``` n = int(input()) a = list(map(int,input().split(' '))) a.sort() reqd=1 days=0 for i in range(len(a)): if a[i]>=reqd: reqd+=1 days+=1 print(days) ```
output
1
83,425
24
166,851
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,426
24
166,852
Tags: data structures, greedy, sortings Correct Solution: ``` n = int(input()) problems = [int(i) for i in input().split()] problems.sort() n = len(problems) myProb = [] j = 1 for i in range(n): number = problems[i] if(j<=number): myProb.append(i+1) j+=1 print(len(myProb)) ```
output
1
83,426
24
166,853
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,427
24
166,854
Tags: data structures, greedy, sortings Correct Solution: ``` n=int(input()) l=[int(x) for x in input().split()] l.sort() j=1 count=0 for i in range(n): if l[i]>=j: count+=1 j+=1 print(count) ```
output
1
83,427
24
166,855
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,428
24
166,856
Tags: data structures, greedy, sortings Correct Solution: ``` import sys, os f = lambda:list(map(int,input().split())) if 'local' in os.environ : sys.stdin = open('./input.txt', 'r') def solve(): n = f()[0] a = f() a = sorted(a) ans = 0 k = 1 for i in a: if i >= k: ans += 1 k += 1 print(ans) solve() ```
output
1
83,428
24
166,857
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,429
24
166,858
Tags: data structures, greedy, sortings Correct Solution: ``` n= int(input()) S= list(map(int,input().split())) ans=0 S.sort() i = 0 day=0 while i<n: if (S[i] >= day+1): ans+=1 day+=1 i+=1 print(ans) ```
output
1
83,429
24
166,859
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,430
24
166,860
Tags: data structures, greedy, sortings Correct Solution: ``` s = int(input()) l = list(map(int,input().split(" "))) l.sort() p = 1 for x in l: if p <= x: p += 1 print(p-1) ```
output
1
83,430
24
166,861
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2
instruction
0
83,431
24
166,862
Tags: data structures, greedy, sortings Correct Solution: ``` import io, sys, atexit, os import math as ma from sys import exit from decimal import Decimal as dec from itertools import permutations from itertools import combinations def li (): return list (map (int, input ().split ())) def num (): return map (int, input ().split ()) def nu (): return int (input ()) def find_gcd ( x, y ): while (y): x, y = y, x % y return x def lcm ( x, y ): gg = find_gcd (x, y) return (x * y // gg) mm = 1000000007 yp = 0 def solve (): t = 1 for _ in range (t): n=nu() a=li() a.sort() cc=0 day=1 for i in range(n): if(a[i]<day): continue cc+=1 day+=1 print(cc) if __name__ == "__main__": solve () ```
output
1
83,431
24
166,863
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` import collections def fun(n,num): s = list(num) #1,1,1,3,3,3 dic = collections.defaultdict(int) for number in s: dic[number] += 1 ans = 1 for i in sorted(dic.keys()): # print(i) while ans <= i and dic[i] > 0: if ans < i: dic[i] -= 1 else: dic[i] = 0 ans += 1 # print(dic) return ans - 1 # brute force and ζ¨‘ζ‹Ÿ n = map(int,input().split()) num = map(int,input().split()) print(fun(n, num)) ```
instruction
0
83,432
24
166,864
Yes
output
1
83,432
24
166,865
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` n = int(input()) a = sorted(map(int, input().split())) ans = 1 for i in range(n): if a[i] >= ans: ans += 1 print(ans - 1) ```
instruction
0
83,433
24
166,866
Yes
output
1
83,433
24
166,867
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` n=int(input()) x=list(map(int,input().split())) sum=0 x.sort() for i in x: if i>sum: sum+=1 print(sum) ```
instruction
0
83,434
24
166,868
Yes
output
1
83,434
24
166,869
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` n = int(input()) a = sorted(list(map(int, input().split()))) q1, q2 = 1, 0 while len(a) > q2: while len(a) > q2 and a[q2] < q1: q2 += 1 if len(a) > q2: q1, q2 = q1+1, q2+1 print(q1-1) ```
instruction
0
83,435
24
166,870
Yes
output
1
83,435
24
166,871
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` # senWithPol = '''Spacious and very nice room : 0.9773643612861633 # It was just a wonderful and amazing stay for us : 0.9333466291427612 # The room was very modern : 0.6510696411132812 # Staff assisted us always for any needs : 0.40595635771751404 # Good location with easy access so that we can go anywhere : 0.854931652545929 # Good stay for us : 0.6618574857711792 # Let’s be honest it’s India so different rules apply but we were very happy to be in a clean efficient hotel so close to one of the best roof top bar( sweet dreams) and the shopping tourist zone of the old city : 0.9793137907981873 # Very happy with the service : 0.9880290627479553 # Great restaurant across the road called Ganesh on the city wall : 0.8149070739746094 # Great value! : 0.7637000679969788 # The best part about this place is food and gym trainer : 0.9360846877098083 # Ankit sir trains us with his best equipments and also gives diet suggestions : 0.8298746943473816 # Love the place : 0.8851836919784546 # I honestly didn't expect that my stay in Patna would be so much fun! And the food is also delicious : 0.8516430854797363 # The staff is also good and they are always there to help : 0.9142824411392212 # The services are not slow : 0.5784827470779419 # Enjoyed the hospitality, food was good, managment of Lemon Tree Premiere at patna is highly responsive and took best care of all the guest : 0.997603714466095 # I do appreciate them : 0.8870315551757812 # Special Thanks to Vishal ji and team, Mr : 0.9889621138572693 # Atish, Mr : 0.42134812474250793 # Ranjit, Mr : 0.6187387704849243 # Dilip, and entire team for making our event a memorable one : 0.8645939826965332 # Regards to MD Sir and GM Sir : 0.7479535937309265 # Thanks from our entire family : 0.9853392839431763 # Gaurav Baidya Agarwal : 0.445486456155777 # I stayed in lemon tree premiere patna was grate stay rooms was very decent and housekeeping staffs very good I could like to recognise one staff from reception Simran she was really helpful and supportive : 0.9676694273948669 # The best part about this place is food and gym trainer : 0.9360846877098083 # Ankit sir trains us with his best equipments and also gives diet suggestions : 0.8298746943473816 # Love the place : 0.8851836919784546 # I honestly didn't expect that my stay in Patna would be so much fun! And the food is also delicious : 0.8516430854797363 # The staff is also good and they are always there to help : 0.9142824411392212 # The services are not slow : 0.5784827470779419 # Enjoyed the hospitality, food was good, managment of Lemon Tree Premiere at patna is highly responsive and took best care of all the guest : 0.997603714466095 # I do appreciate them : 0.8870315551757812 # Special Thanks to Vishal ji and team, Mr : 0.9889621138572693 # Atish, Mr : 0.42134812474250793 # Ranjit, Mr : 0.6187387704849243 # Dilip, and entire team for making our event a memorable one : 0.8645939826965332 # Regards to MD Sir and GM Sir : 0.7479535937309265 # Thanks from our entire family : 0.9853392839431763 # Gaurav Baidya Agarwal : 0.445486456155777 # I stayed in lemon tree premiere patna was grate stay rooms was very decent and housekeeping staffs very good I could like to recognise one staff from reception Simran she was really helpful and supportive : 0.9676694273948669''' # everySenInRev = list() # predVal = list() # for i in senWithPol.split('\n'): # everySenInRev.append(i.split(':')[0]) # predVal.append(i.split(':')[1]) # # print(predVal) # # print(everySenInRev) # dict = {"Sentence": everySenInRev, "Prediction Value": predVal} # df = pd.DataFrame(dict) # df.to_csv('Sentence Sentiment.csv') # 4 # 3 1 4 1 n = int(input()) test = [int(i) for i in input().split()] test = set(test) test = sorted(list(test)) count = 0 for i in range(len(test)): if test[i] < (i+1): break else: count += 1 print(count) ```
instruction
0
83,436
24
166,872
No
output
1
83,436
24
166,873
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` import heapq n = map(int, input().strip()) nums = [int(i) for i in input().strip().split()] the_set = set() new_nums = [] for i in nums: if i not in the_set: new_nums.append(i) the_set.add(i) heapq.heapify(new_nums) ans = 0 for i in range(200001): if new_nums == []: break next_val = heapq.heappop(new_nums) result = next_val - i if next_val >= i: ans = i if result >= i: heapq.heappush(new_nums,result) print(ans) ```
instruction
0
83,437
24
166,874
No
output
1
83,437
24
166,875
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` n = int(input()) a = list(map(int,input().split())) a.sort() ans = 1 for k in range(len(a)): try: if a[k] >= k+1: del a[k] ans += 1 else: for j in range(k+1,len(a)): if a[j] >= k+1: del a[j] ans += 1 break except IndexError: break print(ans) ```
instruction
0
83,438
24
166,876
No
output
1
83,438
24
166,877
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day β€” exactly 2 problems, during the third day β€” exactly 3 problems, and so on. During the k-th day he should solve k problems. Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of contests. The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the number of problems in the i-th contest. Output Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally. Examples Input 4 3 1 4 1 Output 3 Input 3 1 1 1 Output 1 Input 5 1 1 1 2 2 Output 2 Submitted Solution: ``` ll=lambda:map(int,input().split()) t=lambda:int(input()) ss=lambda:input() lx=lambda x:map(int,input().split(x)) #from math import log10 ,log2,ceil,factorial as fac,gcd #from itertools import combinations_with_replacement as cs #from functools import reduce #from bisect import bisect_right as br,bisect_left as bl #from collections import Counter #for _ in range(t()): n=t() a=list(set(ll())) a.sort() k=1 for i in a: if k>i: break k+=1 print(k-1) ```
instruction
0
83,439
24
166,878
No
output
1
83,439
24
166,879
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is editing a complicated computer program. First, variable x is declared and assigned to 0. Then there are instructions of two types: 1. set y v β€” assign x a value y or spend v burles to remove that instruction (thus, not reassign x); 2. if y ... end block β€” execute instructions inside the if block if the value of x is y and ignore the block otherwise. if blocks can contain set instructions and other if blocks inside them. However, when the value of x gets assigned to s, the computer breaks and immediately catches fire. Polycarp wants to prevent that from happening and spend as few burles as possible. What is the minimum amount of burles he can spend on removing set instructions to never assign x to s? Input The first line contains two integers n and s (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^5) β€” the number of lines in the program and the forbidden value of x. The following n lines describe the program. Each line is one of three types: 1. set y v (0 ≀ y ≀ 2 β‹… 10^5, 1 ≀ v ≀ 10^9); 2. if y (0 ≀ y ≀ 2 β‹… 10^5); 3. end. Each if instruction is matched by an end instruction. Each end instruction has an if instruction to match. Output Print a single integer β€” the minimum amount of burles Polycarp can spend on removing set instructions to never assign x to s. Examples Input 5 1 set 1 10 set 2 15 if 2 set 1 7 end Output 17 Input 7 2 set 3 4 if 3 set 10 4 set 2 7 set 10 1 end set 4 2 Output 4 Input 9 200 if 0 set 5 5 if 5 set 100 13 end if 100 set 200 1 end end Output 1 Input 1 10 set 1 15 Output 0 Submitted Solution: ``` m,n=map(int,input().split()) dic={'h':[]} count=0 p=[] while count!=m: l=list(map(str,input().split())) count+=1 if l[0]=='set': dic['h'].append(l) elif l[0]=='if' and count!=1: z=l[1] dic[l[1]]=l while l[0]!='end': l=list(map(str,input().split())) dic[z].append(l) count+=1 elif l[0]=='if' and count==1: u=l[1] dic[u]=[l[0],l[1]] while l[0]!='end': l=list(map(str,input().split())) dic[u].append(l) count+=1 res=0 if dic['h']==[]: for k in dic.keys(): for j in range(2,(len(dic[k])-1)): if int(dic[k][j][1])==n: y=int(dic[k][j][2]) p.append(y) t=int(k[2]) r=res r+=y res+=t if r < res: res=r for i in dic['h']: if i[0]=='set' and int(i[1])==n: res+=int(i[2]) else: if i[1] in dic.keys(): for j in range(2,(len(dic[i[1]])-1)): if int(dic[i[1]][j][1])==n: y=int(dic[i[1]][j][2]) p.append(y) t=int(i[2]) r=res r+=y res+=t if r < res: res=r if p==[]: print(res) else: print(min(p)) ```
instruction
0
83,605
24
167,210
No
output
1
83,605
24
167,211
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is editing a complicated computer program. First, variable x is declared and assigned to 0. Then there are instructions of two types: 1. set y v β€” assign x a value y or spend v burles to remove that instruction (thus, not reassign x); 2. if y ... end block β€” execute instructions inside the if block if the value of x is y and ignore the block otherwise. if blocks can contain set instructions and other if blocks inside them. However, when the value of x gets assigned to s, the computer breaks and immediately catches fire. Polycarp wants to prevent that from happening and spend as few burles as possible. What is the minimum amount of burles he can spend on removing set instructions to never assign x to s? Input The first line contains two integers n and s (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^5) β€” the number of lines in the program and the forbidden value of x. The following n lines describe the program. Each line is one of three types: 1. set y v (0 ≀ y ≀ 2 β‹… 10^5, 1 ≀ v ≀ 10^9); 2. if y (0 ≀ y ≀ 2 β‹… 10^5); 3. end. Each if instruction is matched by an end instruction. Each end instruction has an if instruction to match. Output Print a single integer β€” the minimum amount of burles Polycarp can spend on removing set instructions to never assign x to s. Examples Input 5 1 set 1 10 set 2 15 if 2 set 1 7 end Output 17 Input 7 2 set 3 4 if 3 set 10 4 set 2 7 set 10 1 end set 4 2 Output 4 Input 9 200 if 0 set 5 5 if 5 set 100 13 end if 100 set 200 1 end end Output 1 Input 1 10 set 1 15 Output 0 Submitted Solution: ``` def cost(instructions, start, stop, forbidden): cost = 0 for i in range(start, stop): j = instructions[i] if j[0] == 0 and j[1] == forbidden: cost += j[2] return cost def erase(instructions, start, stop=None): if stop is None: instructions[start][0] = -1 return for i in range(start, stop): instructions[i][0] = -1 n, forbidden = map(int, input().split()) instructions = [] for _ in range(n): *i, = map(int, input().replace('set', '0').replace('if', '1').replace('end', '2').split()) instructions += [i] stack = [] for i, j in enumerate(instructions): if j[0] == 1: stack.append(i) if j[0] == 2: if_start = stack.pop() if_instruction = instructions[if_start] if_value = if_instruction[1] if_cost = cost(instructions, if_start + 1, i, forbidden) for q in range(if_start - 1, -1, -1): w = instructions[q] if w[0] == 1 or w[0] == 2: # print("Disaster!") break if w[0] == 0 and w[1] == if_value: not_if_cost = w[2] if not_if_cost <= if_cost: w[1] = forbidden erase(instructions, if_start, i + 1) else: instructions[if_start] = [0, forbidden, if_cost] erase(instructions, if_start + 1, i + 1) # print(stack); print(instructions); print() final_cost = cost(instructions, 0, len(instructions), forbidden) print(final_cost) ```
instruction
0
83,606
24
167,212
No
output
1
83,606
24
167,213
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is editing a complicated computer program. First, variable x is declared and assigned to 0. Then there are instructions of two types: 1. set y v β€” assign x a value y or spend v burles to remove that instruction (thus, not reassign x); 2. if y ... end block β€” execute instructions inside the if block if the value of x is y and ignore the block otherwise. if blocks can contain set instructions and other if blocks inside them. However, when the value of x gets assigned to s, the computer breaks and immediately catches fire. Polycarp wants to prevent that from happening and spend as few burles as possible. What is the minimum amount of burles he can spend on removing set instructions to never assign x to s? Input The first line contains two integers n and s (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^5) β€” the number of lines in the program and the forbidden value of x. The following n lines describe the program. Each line is one of three types: 1. set y v (0 ≀ y ≀ 2 β‹… 10^5, 1 ≀ v ≀ 10^9); 2. if y (0 ≀ y ≀ 2 β‹… 10^5); 3. end. Each if instruction is matched by an end instruction. Each end instruction has an if instruction to match. Output Print a single integer β€” the minimum amount of burles Polycarp can spend on removing set instructions to never assign x to s. Examples Input 5 1 set 1 10 set 2 15 if 2 set 1 7 end Output 17 Input 7 2 set 3 4 if 3 set 10 4 set 2 7 set 10 1 end set 4 2 Output 4 Input 9 200 if 0 set 5 5 if 5 set 100 13 end if 100 set 200 1 end end Output 1 Input 1 10 set 1 15 Output 0 Submitted Solution: ``` # Ω„ΫŒΪ― Ω…ΨΉΩ„ΩˆΩ„Ψ§Ω† Ψ°Ω‡Ω†ΫŒ ```
instruction
0
83,607
24
167,214
No
output
1
83,607
24
167,215
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarp is editing a complicated computer program. First, variable x is declared and assigned to 0. Then there are instructions of two types: 1. set y v β€” assign x a value y or spend v burles to remove that instruction (thus, not reassign x); 2. if y ... end block β€” execute instructions inside the if block if the value of x is y and ignore the block otherwise. if blocks can contain set instructions and other if blocks inside them. However, when the value of x gets assigned to s, the computer breaks and immediately catches fire. Polycarp wants to prevent that from happening and spend as few burles as possible. What is the minimum amount of burles he can spend on removing set instructions to never assign x to s? Input The first line contains two integers n and s (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ s ≀ 2 β‹… 10^5) β€” the number of lines in the program and the forbidden value of x. The following n lines describe the program. Each line is one of three types: 1. set y v (0 ≀ y ≀ 2 β‹… 10^5, 1 ≀ v ≀ 10^9); 2. if y (0 ≀ y ≀ 2 β‹… 10^5); 3. end. Each if instruction is matched by an end instruction. Each end instruction has an if instruction to match. Output Print a single integer β€” the minimum amount of burles Polycarp can spend on removing set instructions to never assign x to s. Examples Input 5 1 set 1 10 set 2 15 if 2 set 1 7 end Output 17 Input 7 2 set 3 4 if 3 set 10 4 set 2 7 set 10 1 end set 4 2 Output 4 Input 9 200 if 0 set 5 5 if 5 set 100 13 end if 100 set 200 1 end end Output 1 Input 1 10 set 1 15 Output 0 Submitted Solution: ``` n, s = map(int, input().split()) i = 0 v = {} def main(n, s): global i m = 0 while i < n: c = input().split() #print(c, i) #print(v) if c[0] == 'set': if int(c[1]) == s: m += int(c[2]) v[int(c[1])] = (min(int(c[2]), v[int(c[1])]) if (int(c[1]) in v) else int(c[2])) elif c[0] == 'if': #print('deepening', m) i += 1 m += min(int(v[int(c[1])]), main(n, s)) if (int(c[1]) in v) else 0 else: #print('returning', m) return m i += 1 return m print(main(n, s)) ```
instruction
0
83,608
24
167,216
No
output
1
83,608
24
167,217
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,744
24
167,488
Tags: binary search, brute force Correct Solution: ``` s = input() h = [0, 0, 0] for i in s: if i == "B": h[0] += 1 elif i == "S": h[1] += 1 elif i == "C": h[2] += 1 n = [int(x) for x in input().split()] p = [int(x) for x in input().split()] cena = int(input()) l, r = 0, 10 ** 13 while r - l > 1: m = (r + l) // 2 cost = 0 for i in range(3): cost += p[i] * max(0, h[i] * m - n[i]) if cost <= cena: l = m else: r = m print(l) ```
output
1
83,744
24
167,489
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,745
24
167,490
Tags: binary search, brute force Correct Solution: ``` s=input() mp=[0]*200 for i in s : mp[ord(i)]+=1 nb,ns,nc=map(int,input().split()) pb,ps,pc=map(int,input().split()) x=int(input()) l=0 r=1000000000000000000 ans=0 while(l<=r): md=(l+r)//2 temp=x plusB=0 checkB=1 if(md*mp[ord('B')]>nb): plusB+=md*mp[ord('B')]-nb plusB*=pb if(plusB<=temp): temp-=plusB else: checkB=0 plusS=0 checkS=1 if(md*mp[ord('S')]>ns): plusS+=md*mp[ord('S')]-ns plusS *= ps if(plusS<=temp): temp-=plusS else: checkS=0 plusC=0 checkC=1 if(md*mp[ord('C')]>nc): plusC+=md*mp[ord('C')]-nc plusC *= pc if(plusC<=temp): temp-=plusC else: checkC=0 if(checkB==1 and checkS == 1 and checkC==1): ans=max(ans,md) l=md+1 else : r=md-1 print(ans) ```
output
1
83,745
24
167,491
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,746
24
167,492
Tags: binary search, brute force Correct Solution: ``` recipe = input() k_lst = [int(x) for x in input().split(" ")] p_lst = [int(x) for x in input().split(" ")] r = int(input()) recipe_lst = [recipe.count('B'), recipe.count('S'), recipe.count('C')] number , cost , test2 = 0 , 0 , 0 test = True for i in range(3): if recipe_lst[i] > 0: test2 += 1 cost += recipe_lst[i] * p_lst[i] def mk1(): global k_lst, number for x in range(3): k_lst[x] -= recipe_lst[x] number += 1 while test: if k_lst[0] >= recipe_lst[0] and k_lst[1] >= recipe_lst[1] and k_lst[2] >= recipe_lst[2] : mk1() else : temp = 0 for j in range(3): if k_lst[j] < recipe_lst[j] != 0: if r >= (recipe_lst[j] - k_lst[j]) * p_lst[j] : r = r - p_lst[j] * (recipe_lst[j] - k_lst[j]) k_lst[j] += (recipe_lst[j] - k_lst[j]) temp += 1 else: test = False break if temp == test2: mk1() test = False break print(number + r//cost) ```
output
1
83,746
24
167,493
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,747
24
167,494
Tags: binary search, brute force Correct Solution: ``` s=input() bread=s.count("B") cheese=s.count("C") sauce=s.count("S") b,s,c=map(int,input().split()) pb,ps,pc=map(int,input().split()) amount=int(input()) lo=0 hi=10**14 def bs(m): r=amount tempbread=bread*m tempcheese=cheese*m tempsauce=sauce*m if tempbread>b: reqb=tempbread-b if pb*reqb>r: return False r-=reqb*pb if tempcheese>c: reqc=tempcheese-c if pc*reqc>r: return False r-=reqc*pc if tempsauce>s: reqs=tempsauce-s if ps*reqs>r: return False return True while(lo<=hi): m=lo+(hi-lo)//2 if bs(m): lo=m+1 else: hi=m-1 print(hi) ```
output
1
83,747
24
167,495
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,748
24
167,496
Tags: binary search, brute force Correct Solution: ``` recipe = list(input()) nb, ns, nc = [int(x) for x in input().split()] pb, ps, pc = [int(x) for x in input().split()] r = int(input()) b = recipe.count('B') s = recipe.count('S') c = recipe.count('C') rate = [] if b != 0: rateb = int(nb/b) rate.append(rateb) if s != 0: rates = int(ns/s) rate.append(rates) if c != 0: ratec = int(nc/c) rate.append(ratec) m = min(rate) nb -= m * b ns -= m * s nc -= m * c rr = r + nb * pb + nc * pc + ps * ns num = int(rr/(pb * b + ps * s + pc * c)) x = 0 while x == 0: money = pb * max(b * num - nb, 0) + ps * max(s * num - ns,0) + pc * max(num * c - nc,0) if r >= money: x = 1 num -= 1 print(num + m + 1) ```
output
1
83,748
24
167,497
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,749
24
167,498
Tags: binary search, brute force Correct Solution: ``` from collections import defaultdict rec = list(input()) d = defaultdict(int) for elem in rec: d[elem]+=1 nb , ns, nc = map(int, input().split()) pb, ps ,pc = map(int, input().split()) r = int(input()) b, s, c = d['B'], d['S'], d['C'] l, h = 0, r+3*nb while h-l>1: mid = (l+h)//2 t=r if mid*b>nb: t-=(mid*b-nb)*pb if mid*s>ns: t-=(mid*s-ns)*ps if mid*c>nc: t-=(mid*c-nc)*pc if t>=0: l = mid else: h = mid print(l) ```
output
1
83,749
24
167,499
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,750
24
167,500
Tags: binary search, brute force Correct Solution: ``` a = input() br, sr, cr = a.count("B"), a.count("S"), a.count("C") b, s, c = map(int, input().split()) bc, sc, cc = map(int, input().split()) m = int(input()) def ok(x): tm = m tm -= max(0, (br * x - b)) * bc tm -= max(0, (sr * x - s)) * sc tm -= max(0, (cr * x - c)) * cc return tm >= 0 lo = 0 hi = 100000000000000 while lo < hi: mi = (lo + hi + 1)//2 if ok(mi): lo = mi else: hi = mi-1 print(lo) ```
output
1
83,750
24
167,501
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001
instruction
0
83,751
24
167,502
Tags: binary search, brute force Correct Solution: ``` def main(): s = input() x, y, z = map(int, input().split()) p1, p2, p3 = map(int, input().split()) a = s.count('B') b = s.count('S') c = s.count('C') r = int(input()) low = 1 high = 10 ** 18 while low < high: mid = (low + high) // 2 cost = max(0, (mid * a - x) * p1) + max(0, (mid * b - y) * p2) + max(0, (mid * c - z) * p3) if cost <= r: low = mid + 1 else: high = mid print(low - 1) if __name__ == '__main__': main() ```
output
1
83,751
24
167,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` s=list(input()) a=[s.count(c) for c in 'BSC'] n=list(map(int,input().split())) p=list(map(int,input().split())) t=int(input()) l=0 r=10**13 while(l+1<r): mid=(l+r)//2 c=0 for i in range(3): c+=(p[i]*max(a[i]*mid - n[i],0)) if(c>t): r=mid else: l=mid print(l) ```
instruction
0
83,752
24
167,504
Yes
output
1
83,752
24
167,505
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` def check(m): moneyrq = 0 if rqb>0: moneyrq += max(0,(m*rqb-nb)*pb) if rqs>0: moneyrq += max(0,(m*rqs-ns)*ps) if rqc>0: moneyrq += max(0,(m*rqc-nc)*pc) return moneyrq<=have R = lambda: list(map(int,input().split())) s = input() rqb,rqs,rqc = s.count('B'),s.count('S'),s.count('C') nb,ns,nc = R() pb,ps,pc = R() have = int(input()) lo,hi = 0,int(1e18) while lo<hi: m = (lo+hi)//2 if check(m): lo = m+1 else: hi = m if not check(lo): lo -= 1 print(lo) ```
instruction
0
83,753
24
167,506
Yes
output
1
83,753
24
167,507
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` s = input().rstrip() nBread, nSausage, nCheese = map(int, input().split()) priceBread, priceSausage, priceCheese = map(int, input().split()) money = int(input()) def canBuy(n): cost = max(needBread * n - nBread, 0) * priceBread + \ max(needSausage * n - nSausage, 0) * priceSausage + \ max(needCheese * n - nCheese, 0) * priceCheese return cost <= money needBread, needSausage, needCheese = s.count('B'), s.count('S'), s.count('C') left, right = 0, 10 ** 18 while left + 1 < right: mid = (left + right) >> 1 if canBuy(mid): left = mid else: right = mid print(left) ```
instruction
0
83,754
24
167,508
Yes
output
1
83,754
24
167,509
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` import sys,math from collections import deque,defaultdict import operator as op from functools import reduce from itertools import permutations import heapq #sys.setrecursionlimit(10**7) # OneDrive\Documents\codeforces I=sys.stdin.readline alpha="abcdefghijklmnopqrstuvwxyz" mod=10**9 + 7 """ x_move=[-1,0,1,0,-1,1,1,-1] y_move=[0,1,0,-1,1,1,-1,-1] """ def ii(): return int(I().strip()) def li(): return list(map(int,I().strip().split())) def mi(): return map(int,I().strip().split()) def ncr(n, r): r = min(r, n-r) numer = reduce(op.mul, range(n, n-r, -1), 1) denom = reduce(op.mul, range(1, r+1), 1) return numer // denom def ispali(s): i=0 j=len(s)-1 while i<j: if s[i]!=s[j]: return False i+=1 j-=1 return True def isPrime(n): if n<=1: return False elif n<=2: return True else: for i in range(2,int(n**.5)+1): if n%i==0: return False return True def main(): s=I().strip() nb,ns,nc=mi() pb,ps,pc=mi() r=ii() bread=s.count("B") souce=s.count("S") chese=s.count("C") def possi(burger): need_bread=max(0,(burger*bread)-nb) need_souce=max(0,(burger*souce)-ns) need_chese=max(0,(burger*chese)-nc) need_money=(need_bread*pb) + (need_souce*ps)+ (need_chese*pc) if need_money<=r: return True return False i=0 j=4*(10**12) ans=0 while i<=j: mid=i+(j-i)//2 if possi(mid): ans=mid i=mid+1 else: j=mid-1 print(ans) if __name__ == '__main__': main() ```
instruction
0
83,755
24
167,510
Yes
output
1
83,755
24
167,511
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` s= input() nb, ns, nc= map(int, input().split()) pb, ps, pc= map(int, input().split()) rup = int(input()) qb, qs, qc = 0, 0, 0 for h in range(len(s)): if s[h] == "B": qb += 1 elif s[h] == "S": qs += 1 else: qc += 1 l = 0 r = nb + ns + nc + rup ma = l while l <= r: rest = rup mid = (l + r) // 2 #print(((nb * pb) + rest) // (pb* qb), "####", rest,"***",((nc * pc) + rest) // (pc* qc), "####", rest,"***", mid) if qb > 0: if ((nb * pb) + rest) // (pb* qb) >= mid: rest -= ((mid * qb - nb) * pb) else: rest = -1 if rest < 0: r = mid - 1 continue if qs > 0: if ((ns * ps) + rest) // (ps* qs) >= mid: rest -= ((mid * qs - ns) * ps) else: rest = -1 if rest < 0: r = mid - 1 continue if qc > 0: if ((nc * pc) + rest) // (pc* qc) >= mid: rest -= ((mid * qc - nc) * pc) else: rest = -1 if rest < 0: r = mid - 1 continue if rest >= 0: #print(ma,"1111111111111111111") ma = max(ma, mid) l= mid + 1 print(ma) ```
instruction
0
83,756
24
167,512
No
output
1
83,756
24
167,513
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` recipe = input() quantity_b, quantity_s, quantity_c = map(int, input().split()) cost_b, cost_s, cost_c = map(int, input().split()) money = int(input()) recipe_b, recipe_s, recipe_c = 0, 0, 0 for i in range(len(recipe)): if recipe[i] == 'B': recipe_b += 1 elif recipe[i] == 'S': recipe_s += 1 else: recipe_c += 1 def Pred(amount): b = cost_b * recipe_b * amount if quantity_b <= recipe_b: b -= quantity_b * cost_b else: b = 0 s = cost_s * recipe_s * amount if quantity_s <= recipe_s: s -= quantity_s * cost_s else: s = 0 c = cost_c * recipe_c * amount if quantity_c <= recipe_c: c -= quantity_c * cost_c else: c = 0 return b + s + c <= money start, stop = 0, 10 ** 18 while start + 1 < stop: mid = (start + stop) // 2 if Pred(mid): start = mid else: stop = mid print(start) ```
instruction
0
83,757
24
167,514
No
output
1
83,757
24
167,515
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` s = input() b,s,c = s.count('B'),s.count('S'),s.count('C') n1,n2,n3 = map(int,input().split()) p1,p2,p3 = map(int,input().split()) m = int(input()) u1,u2,u3 = 0,0,0 if b: u1 = n1//b if s: u2 = n2//s if c: u3 = n3//c maxx = max(u1,u2,u3) boo = True if u1==maxx: if m<((u1-u2)*s*p2 + (u1-u3)*c*p3): p1=0 boo = False if boo: if u2>=u3: if m<(u2-u3)*c*p3: p2 = 0 else: if m<(u3-u2)*s*p2: p3 = 0 elif u2==maxx: if m<((u2-u1)*b*p1 + (u2-u3)*c*p3): p2=0 boo = False if boo: if u1>=u3: if m<(u1-u3)*c*p3: p1 = 0 else: if m<(u3-u1)*b*p1: p3 = 0 if u3==maxx: if m<((u3-u2)*s*p2 + (u3-u1)*b*p1): p3=0 boo = False if boo: if u2>=u1: if m<(u2-u1)*b*p1: p2 = 0 else: if m<(u1-u2)*s*p2: p1 = 0 print((m+(n1*p1+n2*p2+n3*p3))//(b*p1+s*p2+c*p3)) ```
instruction
0
83,758
24
167,516
No
output
1
83,758
24
167,517
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) ΠΈ 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "Π’SCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again. Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese. Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient. Input The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C). The second line contains three integers nb, ns, nc (1 ≀ nb, ns, nc ≀ 100) β€” the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≀ pb, ps, pc ≀ 100) β€” the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≀ r ≀ 1012) β€” the number of rubles Polycarpus has. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0. Examples Input BBBSSC 6 4 1 1 2 3 4 Output 2 Input BBC 1 10 1 1 10 1 21 Output 7 Input BSC 1 1 1 1 1 3 1000000000000 Output 200000000001 Submitted Solution: ``` st = input() b = 0 s =0 c= 0 count = 0 for i in range(len(st)): if st[i] == "B": b+=1 elif st[i] == "S": s+=1 elif st[i] == "C": c += 1 aval = list(map( int , input().split())) prices = list(map(int ,input().split())) rubies = int(input()) if b ==0 : aval[0] = 0 if s ==0 : aval[1] = 0 if c ==0 : aval[2] = 0 while (aval[0] >= b and aval[1] >= s and aval[2] >= c): aval[0] = aval[0] - b aval[1] = aval[1] - s aval[2] = aval[2] - c count += 1 print(aval) while(rubies >= 0): if aval[0] == aval[1]== aval[2] == 0 : count+= rubies // (prices[0]+prices[1]+prices[2]) print(count) exit() if aval[0] < b: rubies = rubies - (b - aval[0] )*prices[0] aval[0]= b if aval[1] < s: rubies = rubies - (b - aval[1] )*prices[1] aval[1] = s if aval[2] < c: rubies = rubies - (b - aval[2] )*prices[2] aval [2] = c aval[0] = aval[0] - b aval[1] = aval[1] - s aval[2] = aval[2] - c count += 1 print (count) ```
instruction
0
83,759
24
167,518
No
output
1
83,759
24
167,519
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,359
24
168,718
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` from collections import deque from sys import stdin nV, nE = map(int, stdin.readline().split()) g = [[] for _ in range(nV + 1)] rev = [[] for _ in range(nV + 1)] for _ in range(nE): u, v = map(int, stdin.readline().split()) g[u].append(v) rev[v].append(u) k = int(stdin.readline()) path = list(map(int, stdin.readline().split())) def bfs(s, g): UNDEF = -1 dist = [UNDEF] * (nV + 1) dist[s] = 0 q = deque([s]) while q: v = q.pop() for to in g[v]: if dist[to] == UNDEF: dist[to] = 1 + dist[v] q.appendleft(to) return dist dist = bfs(path[-1], rev) mn = mx = 0 prev = None for v in path: if prev and dist[v] > dist[prev] - 1: mn += 1 mx += 1 if prev and dist[v] == dist[prev] - 1: for to in g[prev]: if to != v and dist[to] == dist[v]: mx += 1 break prev = v print('%d %d' % (mn, mx)) ```
output
1
84,359
24
168,719
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,360
24
168,720
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` ''' Testing Python performance @c1729 solution ''' INF = 10 ** 10 from heapq import heappop, heappush def dijkstra(n, graph, start): """ Uses Dijkstra's algortihm to find the shortest path between in a graph. """ dist, parents = [float("inf")] * n, [-1] * n dist[start] = 0 queue = [(0, start)] while queue: path_len, v = heappop(queue) if path_len == dist[v]: for w, edge_len in graph[v]: if edge_len + path_len < dist[w]: dist[w], parents[w] = edge_len + path_len, v heappush(queue, (edge_len + path_len, w)) return dist, parents def main(): #print = out.append ''' Cook your dish here! ''' # Read input and build the graph n, m = map(int, input().split()) graph = [[] for _ in range(n + 1)] child = [[] for _ in range(n + 1)] for _ in range(m): u, v = map(int, input().split()) graph[v].append((u, 1)) child[u].append(v) k = int(input()) p = [int(pi) for pi in input().split()] dist, parents = dijkstra(n + 1, graph, p[-1]) minner, maxxer = 0, 0 for i in range(k - 1): d = k - i - 1 short = dist[p[i]] good_child = set() for c in child[p[i]]: if dist[c] == short - 1: good_child.add(c) if len(good_child) > 1 or p[i + 1] not in good_child: maxxer += 1 if p[i + 1] not in good_child: minner += 1 print(minner, maxxer) ''' Pythonista fLite 1.1 ''' import sys #from collections import defaultdict, Counter, deque # from bisect import bisect_left, bisect_right # from functools import reduce # import math input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) main() #[main() for _ in range(int(input()))] print(*out, sep='\n') ```
output
1
84,360
24
168,721
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,361
24
168,722
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` import sys from collections import deque def bfs(g,src,d,found): q=deque() q.append(src) d[src]=0 while q: rmv=q.popleft() for child in g[rmv]: if d[child]==-1: d[child]=d[rmv]+1 q.append(child) found[child]=1 elif d[child]==d[rmv]+1: found[child]+=1 n,m=map(int,sys.stdin.readline().split()) g=[] gt=[] for i in range(n): g.append(list()) gt.append(list()) for _ in range(m): u,v=map(int,sys.stdin.readline().split()) u-=1 v-=1 g[u].append(v) gt[v].append(u) k=int(sys.stdin.readline()) p=list(map(int,sys.stdin.readline().split())) for i in range(len(p)): p[i]-=1 d=[-1]*(n) found={} bfs(gt,p[-1],d,found) #print(d) mn=0 mx=0 for i in range(1,k): if d[p[i-1]]-1!=d[p[i]]: mn+=1 mx+=1 elif found.get(p[i-1])!=None and found[p[i-1]]>1: mx+=1 print(mn,mx) ```
output
1
84,361
24
168,723
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,362
24
168,724
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` from collections import deque n, m = [int(x) for x in input().split()] adj = [[] for _ in range(n)] for _ in range(m): a, b = [int(x) for x in input().split()] a -= 1 b -= 1 adj[b].append(a) k = int(input()) path = [int(x)-1 for x in input().split()] on_path = [False]*n next_on_path = [-1]*n for i, v in enumerate(path): on_path[v] = True if i + 1 < len(path): next_on_path[v] = path[i+1] s, t = path[0], path[-1] queue = deque([t]) seen = [False]*n seen[t] = True dist = [-1]*n dist[t] = 0 fast_on_path = [False]*n fast_off_path = [False]*n while len(queue) > 0: cur = queue.popleft() for nb in adj[cur]: if not seen[nb]: seen[nb] = True queue.append(nb) dist[nb] = dist[cur] + 1 if on_path[nb]: if next_on_path[nb] == cur: fast_on_path[nb] = True else: fast_off_path[nb] = True elif on_path[nb] and dist[nb] == dist[cur] + 1: if next_on_path[nb] == cur: fast_on_path[nb] = True else: fast_off_path[nb] = True # print("dist: ", dist) # print("fast_on_path: ", fast_on_path) # print("fast_off_path: ", fast_off_path) print(len(path) - sum(fast_on_path) - 1, sum(fast_off_path)) ```
output
1
84,362
24
168,725
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,363
24
168,726
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` from sys import stdin, stdout import math, sys import os import sys from io import BytesIO, IOBase from collections import defaultdict,deque BUFSIZE = 8192 # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline sys.setrecursionlimit(10 ** 5) 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") adj=defaultdict(list) rev=defaultdict(list) dist=[] visited=[] def bfs(node): global adj, rev, dist, visited visited[node]=1 qu=deque() qu.append(node) while qu: el=qu.popleft() for child in rev[el]: if not visited[child]: dist[child]=dist[el]+1 qu.append(child) visited[child]=1 def main(): global adj,rev,dist,visited n,m=map(int,input().split()) adj.clear() rev.clear() visited=[0]*(n+1) dist=[0]*(n+1) mn=0 mx=0 for i in range(m): u,v=map(int,input().split()) adj[u].append(v) rev[v].append(u) k=int(input()) arr=[int(k) for k in input().split()] no=arr[-1] bfs(no) # print(rev) # print(dist) # nx=[0]*(n+1) # nx[arr[0]]=1 for i in range(1,k): if dist[arr[i]]>dist[arr[i-1]]-1: mn+=1 mx+=1 else: for child in adj[arr[i-1]]: if child!=arr[i]: if dist[child]==dist[arr[i]]: mx+=1 break stdout.write(str(mn)+" "+str(mx)+"\n") if __name__=="__main__": main() ```
output
1
84,363
24
168,727
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,364
24
168,728
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` n, m = list(map(int, input().split())) roads = [[] for _ in range(n)] in_roads = [[] for _ in range(n)] def change(x): return int(x)-1 for _ in range(m): a, b = list(map(change, input().split())) roads[a].append(b) in_roads[b].append(a) total_path_dist = int(input()) path = list(map(change, input().split())) from collections import deque min_to_end = [0]*n extended = [False]*n queue = deque() queue.append((path[-1], 0)) extended[path[-1]] = True while queue: i, leng = queue.popleft() min_to_end[i] = leng for in_rd in in_roads[i]: if not extended[in_rd]: extended[in_rd] = True queue.append((in_rd, leng+1)) min_ = 0 max_ = 0 for i in range(len(path)-1): inter = path[i] to_int = path[i+1] best_dist = min_to_end[inter] to_is_min = False other_mins = False for oth_int in roads[inter]: oth_best_dist = min_to_end[oth_int] if oth_int == to_int: if best_dist-1 == oth_best_dist: to_is_min = True else: min_ += 1; max_ += 1 #print(i, inter, oth_int) break elif best_dist-1 == oth_best_dist: other_mins = True if to_is_min and other_mins: max_ += 1 print(min_, max_) ```
output
1
84,364
24
168,729
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,365
24
168,730
Tags: dfs and similar, graphs, shortest paths 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 -------------------- def main(): from collections import deque,defaultdict n,m=map(int,input().split());rev=[[] for s in range(n)] for s in range(m): u,v=map(int,input().split()) u-=1;v-=1 rev[v].append(u) k=int(input());vals=[int(i)-1 for i in input().split()] queue=deque([vals[-1]]);pos=defaultdict(list);pos[vals[-1]]=[0,set([])] while len(queue)>0: v0=queue.popleft() for s in range(len(rev[v0])): node=rev[v0][s] if len(pos[node])==0: pos[node]=[pos[v0][0]+1,set([v0])] queue.append(node) else: if pos[v0][0]+1<pos[node][0]: pos[node]=[pos[v0][0]+1,set([v0])] elif pos[v0][0]+1==pos[node][0]: pos[node][1].add(v0) maxy=0;minny=0 for s in range(k-1): if not(vals[s+1] in pos[vals[s]][1]): maxy+=1;minny+=1 elif vals[s+1] in pos[vals[s]][1] and len(pos[vals[s]][1])>=2: maxy+=1 print(minny,maxy) main() ```
output
1
84,365
24
168,731
Provide tags and a correct Python 3 solution for this coding contest problem. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3
instruction
0
84,366
24
168,732
Tags: dfs and similar, graphs, shortest paths Correct Solution: ``` from collections import deque n,m = map(int,input().split()) g = [list() for _ in range(n)] for _ in range(m): u,v = map(int,input().split()) u,v = u-1,v-1 g[v].append(u) k = int(input()) path = list(map(int,input().split())) for i in range(k): path[i] -= 1 q = deque([]) q.append(path[-1]) dist = [-1]*(n) isv = [-1]*(n) dist[q[0]] = 0 while q: top = q.popleft() for i in g[top]: if dist[i] == -1: q.append(i) dist[i] = dist[top]+1 isv[i] = 1 elif dist[i] == dist[top]+1: isv[i]+=1 mn,mx = 0,0 for i in range(1,k): # if the difference between current and previous not 1 # means that it is not the optimal path # hence the ans += 1 if dist[path[i-1]]-1 != dist[path[i]]: mn+=1 mx += 1 elif isv[path[i-1]] != -1 and isv[path[i-1]]>1: mx += 1 print(mn,mx) ```
output
1
84,366
24
168,733
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3 Submitted Solution: ``` # from debug import debug inf = int(1e10) n, m = map(int, input().split()) trans = [[] for i in range(n)] graph = [[] for i in range(n)] for i in range(m): a,b = map(int, input().split()) trans[b-1].append(a-1) graph[a-1].append(b-1) k = int(input()) lis = [x-1 for x in map(int, input().split())] s, d = lis[0], lis[-1] v = [0]*n dis = [inf]*n v[d] = 1 dis[d] = 0 q = [d] while q: node = q.pop(0) for i in trans[node]: if not v[i] and dis[i] > dis[node]+1: dis[i] = dis[node] + 1 q.append(i) reb, ad = 0, 0 prev = s for i in range(1, k-1): if dis[lis[i]] + 1 > dis[prev]: reb+=1 for j in graph[prev]: if j != lis[i] and dis[j] + 1 == dis[prev]: ad+=1 break prev = lis[i] print(reb, ad) ```
instruction
0
84,367
24
168,734
Yes
output
1
84,367
24
168,735
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3 Submitted Solution: ``` from collections import deque def check(h): ans = 0 for i in range(len(zapadlo[h])): if colors[zapadlo[h][i]] == colors[h]- 1: ans += 1 return ans n, m = map(int, input().split()) mas = [0] * n zapadlo = [0] * n for i in range(n): mas[i] = [] zapadlo[i] = [] for i in range(m): a, b = map(int, input().split()) mas[b - 1] += [a - 1] zapadlo[a - 1] += [b - 1] k = int(input()) path = list(map(int, input().split())) q = deque() v = path[-1] - 1 q.append(v) colors = [-1]*n pred = [-1]*n colors[v] = 0 while q : #is empty f = q.popleft() for j in range(len(mas[f])): i = mas[f][j] if colors[i] == -1 : colors[i] = (colors[f]+1) pred[i] = f q.append(i) a = 0 b = 0 for i in range(1, k): g = path[i] - 1 z = path[i - 1] - 1 e = colors[g] f = colors[z] if e + 1 != f: a += 1 b += 1 else: if check(z) > 1: b += 1 print(a, b) ```
instruction
0
84,368
24
168,736
Yes
output
1
84,368
24
168,737
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths. Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p_1, p_2, ..., p_k, where p_1 = s, p_k = t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let's denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1, 2, 3, 4] (s = 1, t = 4): Check the picture by the link [http://tk.codeforces.com/a.png](//tk.codeforces.com/a.png) 1. When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1, 5, 4]; 2. Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2, 6, 4] (note that it could choose [2, 3, 4]); 3. Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3, 4]; 4. Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2, 3, 4] instead of [2, 6, 4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3, 4] during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers n and m (2 ≀ n ≀ m ≀ 2 β‹… 10^5) β€” the number of intersections and one-way roads in Bertown, respectively. Then m lines follow, each describing a road. Each line contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u, v) appears at most once in these m lines (but if there is a road (u, v), the road (v, u) can also appear). The following line contains one integer k (2 ≀ k ≀ n) β€” the number of intersections in Polycarp's path from home to his workplace. The last line contains k integers p_1, p_2, ..., p_k (1 ≀ p_i ≀ n, all these integers are pairwise distinct) β€” the intersections along Polycarp's path in the order he arrived at them. p_1 is the intersection where Polycarp lives (s = p_1), and p_k is the intersection where Polycarp's workplace is situated (t = p_k). It is guaranteed that for every i ∈ [1, k - 1] the road from p_i to p_{i + 1} exists, so the path goes along the roads of Bertown. Output Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey. Examples Input 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 Output 1 2 Input 7 7 1 2 2 3 3 4 4 5 5 6 6 7 7 1 7 1 2 3 4 5 6 7 Output 0 0 Input 8 13 8 7 8 6 7 5 7 4 6 5 6 4 5 3 5 2 4 3 4 2 3 1 2 1 1 8 5 8 7 5 2 1 Output 0 3 Submitted Solution: ``` from sys import stdin from collections import defaultdict, deque input = stdin.readline n, m = map(int, input().split()) edges = defaultdict(list) forward = defaultdict(list) for _ in range(m): end, start = map(int, input().split()) edges[start].append(end) forward[end].append(start) k = int(input()) path = list(map(int, input().split())) start, end = path[0], path[-1] # bfs from end dist = {end:0} q = deque() q.append(end) while q: node = q.popleft() for nb in edges[node]: if nb in dist: continue dist[nb] = dist[node] + 1 q.append(nb) # iterate MIN, MAX = 0, 0 for idx in range(k): if idx == k - 1: break now = path[idx] next_node = path[idx+1] if dist[next_node] + 1 != dist[now]: MIN += 1 MAX += 1 elif any( dist[child] + 1 == dist[now] for child in forward[now] if child != next_node): MAX += 1 print(MIN, MAX) ```
instruction
0
84,369
24
168,738
Yes
output
1
84,369
24
168,739