message
stringlengths
2
30.5k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
237
109k
cluster
float64
10
10
__index_level_0__
int64
474
217k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` Sum, Ave, Cou = 0, 0, 0 while True : try : a, b = input().split(',') Sum += int(a) * int(b) Ave += int(b) Cou += 1 except : break print(Sum) if Ave / Cou - Ave // Cou >= 0.5 : print(Ave // Cou+1) else : print(Ave//Cou) ```
instruction
0
28,989
10
57,978
Yes
output
1
28,989
10
57,979
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` from decimal import * sum = 0 cnt = 0 nums = 0 while 1: try: price, num = (int(x) for x in input().split(',')) sum += price * num cnt += 1 nums += num except EOFError: break print(sum) nums = Decimal(nums / cnt).quantize(Decimal('0'), rounding=ROUND_HALF_UP) print(nums) ```
instruction
0
28,990
10
57,980
Yes
output
1
28,990
10
57,981
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` try: result = result2 = count = 0 while True: a, b = map(int, input().split(',')) result += a*b result2 += b count += 1 except: print(result) print(round(result2/count+0.5)) ```
instruction
0
28,991
10
57,982
Yes
output
1
28,991
10
57,983
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` A = [] B = [] while True: try: x,y = map(int,input().split(',')) A.append(x) B.append(y) except EOFError: break N = len(A) print(sum(A [i] * B [i] for i in range(N))) print(sum(B) * 2 // N - sum(B) // N) ```
instruction
0
28,992
10
57,984
Yes
output
1
28,992
10
57,985
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` a, b = [], [] while True: try: x, y = map(int, line.strip().split(",")) a, b = a+[x*y], b+[y] except: break print(sum(a)) print(round(sum(b)/len(b))) ```
instruction
0
28,993
10
57,986
No
output
1
28,993
10
57,987
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` import sys s = c = n = 0 for line in sys.stdin: x, y = map(int, line.split(',')) s += x * y c += y n += 1 print(s) print(round(c / n)) ```
instruction
0
28,994
10
57,988
No
output
1
28,994
10
57,989
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` n = 0 sum = 0 ave = 0 while(True): try: a,b = map(int,input().split(",")) sum += a*b ave += b n+=1 except EOFError: print(sum) print("%.0f"%(ave/n)) break ```
instruction
0
28,995
10
57,990
No
output
1
28,995
10
57,991
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Create a program that reads the sales unit price and sales quantity and outputs the total sales amount and the average sales quantity. Input The input is given in the following format: Sales unit price, sales quantity Sales unit price, sales quantity :: :: A comma-separated pair of unit price and quantity is given across multiple lines. All values ​​entered are greater than or equal to 0 and less than or equal to 1,000, and the number of unit price and quantity pairs does not exceed 100. Output Please output the total sales amount (integer) on the first line and the average sales quantity (integer) on the second line. If the average sales volume has a fraction (number after the decimal point), round off to the first decimal place. Example Input 100,20 50,10 70,35 Output 4950 22 Submitted Solution: ``` try: result = result2 = count = 0 while True: a, b = map(int, input().split(',')) result += a*b result2 += b count += 1 except: print(result) print((round(result2/count+0.5)) ```
instruction
0
28,996
10
57,992
No
output
1
28,996
10
57,993
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,557
10
59,114
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` n,s = map(int, input().split()) sell = {} buy = {} for i in range(n): type, price, vol = input().split() price = int(price) vol = int(vol) if type == 'S': sell[price] = sell.get(price, 0) + vol else: buy[price] = buy.get(price, 0) + vol for order in sorted(sell.items(), reverse = True )[-s:]: print("S", order[0], order[1]) for order in sorted(buy.items(), reverse = True )[:s]: print("B", order[0], order[1]) ```
output
1
29,557
10
59,115
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,558
10
59,116
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` def search(a, key): for i in range(len(a)): if key == a[i].price: return i return -1 def insert_max(a, key): size = len(a) i = 0 while i < size: if key.price > a[i].price: break i += 1 for j in reversed(range(i, size-1)): a [j+1] = a[j] if i != size: a[i] = key def insert_min(a, key): size = len(a) i = 0 while i < size: if key.price < a[i].price: break i += 1 for j in reversed(range(i, size-1)): a [j+1] = a[j] if i != size: a[i] = key class Buy: def __init__(self, price, vol): self.price = price self.vol = vol class Sell: def __init__(self, price, vol): self.price = price self.vol = vol n,s = map(int, input().split()) buy = [] sell = [] best_buy = s * [Sell(-1,0)] best_sell = s * [Sell(100001,0)] cb = 0 cs = 0 for i in range(n): d, p, v = input().split() price = int(p) vol = int(v) if d == 'B': ind = search(buy, price) if ind == -1: obj = Buy(price, vol) buy.append(obj) cb += 1 insert_max(best_buy, obj) else: buy[ind].vol += vol else: ind = search(sell, price) if ind == -1: obj = Sell(price, vol) sell.append(obj) cs += 1 insert_min(best_sell, obj) else: sell[ind].vol += vol if cb > s: cb = s if cs > s: cs = s for i in reversed(range(cs)): print('S' + ' ' + str(best_sell[i].price) + ' ' + str(best_sell[i].vol)) for i in range(cb): print('B' + ' ' + str(best_buy[i].price) + ' ' + str(best_buy[i].vol)) ```
output
1
29,558
10
59,117
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,559
10
59,118
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` n = [int(i) for i in input().split()] buy = [] buyP = [] sell = [] sellP = [] for i in range(n[0]): l = input().split() l[1] = int(l[1]) l[2] = int(l[2]) if l[0] == 'B': l.pop(0) buy.append(l) buyP.append([l[0]]) else: l.pop(0) sell.append(l) sellP.append([l[0]]) for i in range(len(buyP)-1,-1,-1): if buyP.count(buyP[i]) > 1: buyP.pop(i) for i in range(len(sellP)-1,-1,-1): if sellP.count(sellP[i]) > 1: sellP.pop(i) buyP.sort() buyP.reverse() sellP.sort() for i in range(len(sellP)): sellP[i].append(0) sellP[i].append(0) for j in sell: if sellP[i][0] == j[0]: sellP[i][1] = 'S' sellP[i][2] += j[1] for z in range(min(n[1],len(sellP))-1,-1,-1): print('S '+ str(sellP[z][0]) +' '+ str(sellP[z][2])) for i in range(len(buyP)): buyP[i].append(0) buyP[i].append(0) for j in buy: if buyP[i][0] == j[0]: buyP[i][1] = 'B' buyP[i][2] += j[1] for z in range(min(n[1],len(buyP))): print('B '+ str(buyP[z][0]) +' '+ str(buyP[z][2])) ```
output
1
29,559
10
59,119
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,560
10
59,120
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` import sys import os def getint(): return list(map(int,input().split(' '))) n,s=getint() dic={'B':{},'S':{}} for i in range(n): d,p,q=list(input().split(' ')) p=int(p) q=int(q) dic[d][p]=dic[d].get(p,0)+q for key in sorted(dic['S'],reverse=True)[-s:]: print('S',key,dic['S'][key]) for key in sorted(dic['B'],reverse=True)[:s]: print('B',key,dic['B'][key]) #for line in sorted(dic['B'])[:s]: #print('B',line) ```
output
1
29,560
10
59,121
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,561
10
59,122
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` from sys import stdin,stdout input = stdin.readline b = {} s = {} n, l = map(int,input().split(' ')) for i in range(n): d,p,q = input().split(' ') p,q = int(p),int(q) if d == 'B': try: b[p] += q except KeyError: b[p] = q else: try: s[p] += q except KeyError: s[p] = q for key in sorted(sorted(s)[0:min(l,len(s))],reverse=True): print("S %s %s" % (key, s[key])) for key in sorted(b,reverse=True)[0:min(l,len(b))]: print("B %s %s" % (key, b[key])) ```
output
1
29,561
10
59,123
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,562
10
59,124
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` from sys import argv from collections import defaultdict import sys def convert(line): ret = [] for e in line: try: ret.append(int(e)) except: ret.append(e) return ret def reader(): for line in sys.stdin: yield convert(line.strip().split(' ')) def main(): r = reader() n, m = next(r) sell = defaultdict(int) buy = defaultdict(int) for transaction, price, quantity in r: if transaction=='B': buy[price]+=quantity if transaction=='S': sell[price]+=quantity sell=sorted(sell.items(), key=lambda x:x[0], reverse=True) buy=sorted(buy.items(), key=lambda x:x[0], reverse=True) for p, q in sell[-m:]: print("S %d %d" % (p,q)) for p, q in buy[:m]: print("B %d %d" % (p,q)) if __name__ == "__main__": main() ```
output
1
29,562
10
59,125
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,563
10
59,126
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` def main(): n, s = map(int, input().split()) tot = {"S": {}, "B": {}} for _ in range(n): tmp = input().split() t, p, q = tot[tmp[0]], int(tmp[1]), int(tmp[2]) t[p] = t.get(p, 0) + q for c in "S", "B": t = tot[c] pp = sorted(t.keys(), reverse=True) for p in pp[-s:] if c == "S" else pp[:s]: print(c, p, t[p]) if __name__ == '__main__': main() # Made By Mostafa_Khaled ```
output
1
29,563
10
59,127
Provide tags and a correct Python 3 solution for this coding contest problem. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
instruction
0
29,564
10
59,128
Tags: data structures, greedy, implementation, sortings Correct Solution: ``` import operator n,s=map(int,input().split()) orders=[] for i in range(n): orders.append(tuple(map(str,input().split()))) #d,p,q aggr={} for order in orders: key=(order[0][0],int(order[1])) if key not in aggr: aggr[key]=0 aggr[key]+=int(order[2]) #volume books={"B":[],"S":[]} for key in aggr: book=books[key[0]] volume=aggr[key] book.append((key[0],key[1],volume)) #d,p,q for k in books: books[k]=list(reversed(sorted(books[k],key=operator.itemgetter(1)))) lenB=len(books["S"]) for i in range(min(lenB,s)): order=books["S"][max(0,lenB-s)+i] print("S %d %d" %(order[1],order[2])) for i in range(min(len(books["B"]),s)): order=books["B"][i] print("B %d %d" %(order[1],order[2])) ```
output
1
29,564
10
59,129
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` n,s=map(int,input().split()) sell=[] buy=[] for i in range(100003): sell.append(0) buy.append(0) for i in range(n): a,b,c=input().split(); if a =='B': buy[int(b)]+=int(c) else: sell[int(b)]+=int(c) it=0 nr=0 while it<s and nr<=100000: if sell[nr]>0: it+=1 nr+=1 while nr>=0: if sell[nr]>0: print('S',nr,sell[nr]) nr-=1 it=0 nr=100000 while it<s and nr>=0: if buy[nr]>0: it+=1 print('B',nr,buy[nr]) nr-=1 ```
instruction
0
29,565
10
59,130
Yes
output
1
29,565
10
59,131
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` s = [0]*100010 b = [0]*100010 temps = [] tempb = [] n,m = map(int,input().split()) for _ in range(n): d,p,q = input().split(' ') if(d=='S'): s[int(p)]+=int(q) else: b[int(p)]+=int(q) for i in range(100010): if(s[i]): temps.append([i,s[i]]) if(b[i]): tempb.append([i,b[i]]) temps = temps[:m] tempb = tempb[::-1] tempb = tempb[:m] #print(temps,tempb) for i in temps[::-1]: print('S',i[0],i[1]) for i in tempb: print('B',i[0],i[1]) ```
instruction
0
29,566
10
59,132
Yes
output
1
29,566
10
59,133
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` n,s=map(int,input().split()) sem=[-1]*(100001) bum=[-1]*(100001) for i in range(n): wh,p,q=input().split() p=int(p) q=int(q) if wh=='B': if bum[p]==-1: bum[p]=0 bum[p]+=q else: if sem[p]==-1: sem[p]=0 sem[p]+=q fins=[] finb=[] ts=s for i in range(100000,-1,-1): if not ts: break if(bum[i]!=-1): finb.append((i,bum[i])) ts-=1 ts=s for i in range(100001): if not ts: break if(sem[i]!=-1): fins.append((i,sem[i])) ts-=1 ts=s for i in range(len(fins)-1,-1,-1): if not ts: break a,b=fins[i] print('S',a,b) ts-=1 ts=s for i in range(len(finb)): if not ts: break a,b=finb[i] print('B',a,b) ts-=1 ```
instruction
0
29,567
10
59,134
Yes
output
1
29,567
10
59,135
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` n,k= map(int, input().split()) bb={} ss={} for i in range(n): a,b,c=input().split() b=int(b) c=int(c) if a=="B": if b in bb: bb[b]+=int(c) else: bb[b]=int(c) else: if b in ss: ss[b]+=int(c) else: ss[b]=int(c) buy=sorted(bb.items(),reverse=True)[:k] sell=reversed(sorted(ss.items())[:k]) for pi,qi in sell: print("S",pi,qi) for pi,qi in buy: print("B",pi,qi) ```
instruction
0
29,568
10
59,136
Yes
output
1
29,568
10
59,137
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` s = [0]*100010 b = [0]*100010 temps = [] tempb = [] n,m = map(int,input().split()) for _ in range(n): d,p,q = input().split(' ') if(d=='S'): s[int(p)]+=int(q) else: b[int(p)]+=int(q) for i in range(100010): if(s[i]): temps.append([i,s[i]]) if(b[i]): tempb.append([i,b[i]]) temps = temps[:m] tempb = tempb[:m] #print(temps,tempb) for i in temps[::-1]: print('S',i[0],i[1]) for i in tempb[::-1]: print('B',i[0],i[1]) ```
instruction
0
29,569
10
59,138
No
output
1
29,569
10
59,139
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` R = lambda: list(map(int,input().split())) from collections import defaultdict mps,mpb = defaultdict(lambda:0), defaultdict(lambda:0) n,m = R() for _ in range(n): s = input().split() t = s[0] x,y = int(s[1]), int(s[2]) if t=='S': mps[x]+=y elif t=='B': mpb[x]+=y ls = sorted(list(mps.items()),key=lambda x:x[0],reverse=True) lb = sorted(list(mpb.items()),key=lambda x:x[0],reverse=True) for x,y in ls[:m]: print('S',x,y) for x,y in lb[:m]: print('B',x,y) #print(list(mps.items())) ```
instruction
0
29,570
10
59,140
No
output
1
29,570
10
59,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` #inp = open("input.txt") n, m = [int(x) for x in input().split()] a, b = {}, {} for i in range(n): s = input().split() if s[0] == "S": if int(s[1]) in a: a[int(s[1])] += int(s[2]) else: a[int(s[1])] = int(s[2]) else: if int(s[1]) in b: b[int(s[1])] += int(s[2]) else: b[int(s[1])] = int(s[2]) k = 0 for x in sorted(a, reverse=True): if k == m: break print("S", x, a[x]) k += 1 k = 0 for x in sorted(b, reverse=True): if k == m: break print("B", x, b[x]) k += 1 ```
instruction
0
29,571
10
59,142
No
output
1
29,571
10
59,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number i has price pi, direction di — buy or sell, and integer qi. This means that the participant is ready to buy or sell qi stocks at price pi for one stock. A value qi is also known as a volume of an order. All orders with the same price p and direction d are merged into one aggregated order with price p and direction d. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth s contains s best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than s aggregated orders for some direction then all of them will be in the final order book. You are given n stock exhange orders. Your task is to print order book of depth s for these orders. Input The input starts with two positive integers n and s (1 ≤ n ≤ 1000, 1 ≤ s ≤ 50), the number of orders and the book depth. Next n lines contains a letter di (either 'B' or 'S'), an integer pi (0 ≤ pi ≤ 105) and an integer qi (1 ≤ qi ≤ 104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2s lines with aggregated orders from order book of depth s. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price x and volume y. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders. Submitted Solution: ``` amount, deep = map(int, input().split()) deep_copy = deep buy_list = [] sell_list = [] counter_1 = 0 while amount != 0: direction, price, amount_pos = map(str, input().split()) contol_list = [direction, int(price), int(amount_pos)] if direction == 'B': buy_list.append(contol_list) else: sell_list.append(contol_list) amount-=1 buy_list.sort() sell_list.sort() while counter_1 < len(buy_list) - 1: if buy_list[counter_1][1] == buy_list[counter_1 + 1][1]: buy_list[counter_1][2] += buy_list[counter_1 + 1][2] del(buy_list[counter_1 + 1]) else: counter_1 += 1 counter_1 = 0 while counter_1 < len(sell_list) - 1: if sell_list[counter_1][1] == sell_list[counter_1 + 1][1]: sell_list[counter_1][2] += sell_list[counter_1 + 1][2] del(sell_list[counter_1 + 1]) else: counter_1 += 1 buy_list.reverse() sell_list.reverse() final_list = [] for i in range(min(deep, len(sell_list))): final_list.append(sell_list[i]) buy_list.reverse() for i in range(len(sell_list)): print(*sell_list[i]) for i in range(min(deep, len(buy_list))): print(*buy_list[i]) ```
instruction
0
29,572
10
59,144
No
output
1
29,572
10
59,145
Provide a correct Python 3 solution for this coding contest problem. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217
instruction
0
29,957
10
59,914
"Correct Solution: ``` from copy import deepcopy from operator import itemgetter def buy(p, cr): cnt, rem = cr r1000 = p % 1000 r500 = p % 500 m500 = 500 - r500 m1000 = 1000 - r1000 if r1000 == 0: if rem < 500: return ((cnt, rem),) else: return ((cnt+1, rem-500),) elif r1000 == 500: return ((cnt+1, rem),) elif r1000 < 500: return ((cnt+1, rem + m500), (cnt, rem + m1000)) else: if rem - r500 >= 0: return ((cnt+1, rem - r500),) else: return ((cnt, rem + m500),) INF = 10**20 def solve(plist): dp = { (0, 0): 0 } for p in plist: ndp = {} for cr in list(dp.keys()): for ncr in buy(p, cr): ndp[ncr] = min(dp[cr] + p, dp.get(ncr, INF)) dp.update(ndp) ndp = {} cc = -1 mp = INF for c, r in sorted(dp.keys(), reverse=True): pp = dp[(c, r)] if cc != c: mp = INF cc = c if mp > pp: ndp[(c, r)] = pp mp = pp dp = ndp cplist = ((t[0][0], t[1]) for t in dp.items()) return sorted(sorted(cplist, key=itemgetter(1)), key=itemgetter(0), reverse=True)[0] def main(): while True: n = int(input().strip()) if n == 0: break plist = [int(input().strip()) for _ in range(n)] print(" ".join(str(i) for i in solve(plist))) if __name__ == '__main__': main() ```
output
1
29,957
10
59,915
Provide a correct Python 3 solution for this coding contest problem. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217
instruction
0
29,958
10
59,916
"Correct Solution: ``` from collections import defaultdict def main(n): m = n * 499 + 1 dp = defaultdict(list) dp[0] = [0, 0] for _ in range(n): ndp = defaultdict(lambda:[-1,-1]) for key, value in dp.items(): ndp[key] = value[:] p = int(input()) a = -p % 1000 if a >= 500: b = a - 500 for j in dp.keys(): if dp[j][0] < 0: continue tmp = ndp[j + b] if tmp[0] < dp[j][0] + 1: tmp[0] = dp[j][0] + 1 tmp[1] = dp[j][1] + p elif tmp[0] == dp[j][0] + 1 and tmp[1] > dp[j][1] + p: tmp[0] = dp[j][0] + 1 tmp[1] = dp[j][1] + p else: b = 500 - a % 500 for j in dp.keys(): if dp[j][0] < 0: continue tmp = ndp[j + a] if tmp[0] < dp[j][0]: tmp[0] = dp[j][0] tmp[1] = dp[j][1] + p elif tmp[0] == dp[j][0] and tmp[1] > dp[j][1] + p: tmp[1] = dp[j][1] + p if j - b >= 0: tmp = ndp[j - b] if tmp[0] < dp[j][0] + 1: tmp[0] = dp[j][0] + 1 tmp[1] = dp[j][1] + p elif tmp[0] == dp[j][0] + 1 and tmp[1] > dp[j][1] + p: tmp[1] = dp[j][1] + p dp = ndp ans = [0, 0] for n, c in dp.values(): if n > ans[0]: ans = [n, c] elif n == ans[0] and c < ans[1]: ans = [n, c] print(*ans) if __name__ == "__main__": while 1: n = int(input()) if n: main(n) else: break ```
output
1
29,958
10
59,917
Provide a correct Python 3 solution for this coding contest problem. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217
instruction
0
29,959
10
59,918
"Correct Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 mod = 10**9+9 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def main(): rr = [] def f(n): a = [I() for _ in range(n)] t = [(0,0,0)] for c in a: nt = collections.defaultdict(lambda: inf) c1 = c % 1000 c5 = c % 500 mc5 = 500 - c5 for g,k,p in t: nt[(g,k)] = p if c1 == 0: for g,k,p in t: if k >= 500 and nt[(g+1,k-500)] > p + c: nt[(g+1,k-500)] = p + c elif c1 == 500: for g,k,p in t: if nt[(g+1, k)] > p + c: nt[(g+1, k)] = p + c elif c1 < 500: for g,k,p in t: if nt[(g+1, k+mc5)] > p + c: nt[(g+1, k+mc5)] = p + c else: for g,k,p in t: if k + mc5 >= 500 and nt[(g+1,k+mc5-500)] > p + c: nt[(g+1,k+mc5-500)] = p + c if nt[(g, k+mc5)] > p + c: nt[(g, k+mc5)] = p + c t = [] cg = -1 mk = -1 mp = inf # print('nt',nt) for g,k in sorted(nt.keys(), reverse=True): p = nt[(g,k)] if p == inf: continue if cg != g: mp = inf cg = g if mk < k or mp > p: t.append((g,k,p)) if mk < k: mk = k if mp > p: mp = p # print(len(t)) r = 0 rp = inf for g,k,p in t: if r < g or (r==g and rp > p): r = g rp = p return '{} {}'.format(r, rp) while 1: n = I() if n == 0: break rr.append(f(n)) return '\n'.join(map(str, rr)) print(main()) ```
output
1
29,959
10
59,919
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Submitted Solution: ``` MAX_COINS = 500 * 100 INF = 10 ** 9 def dfs(idx, coins): if idx == N: return 0, 0 elif dp[idx][coins] != -1: return dp[idx][coins] ret = (0, -INF) # not buy ret = max(ret, dfs(idx + 1, coins)) # buy using only bills change = (1000 - P[idx] % 1000) % 1000 cand, money = dfs(idx + 1, coins + change % 500) ret = max(ret, (cand + (change >= 500), money - P[idx])) # buy using both and get 500 if (P[idx] + 500) % 1000 <= coins: cand, money = dfs(idx + 1, coins - (P[idx] + 500) % 1000) ret = max(ret, (cand + 1, money - P[idx])) dp[idx][coins] = ret # print(idx, coins, ret) return ret while True: N = int(input()) if not N: break P = [int(input()) for _ in range(N)] dp = [[-1] * (MAX_COINS + 1) for _ in range(N + 1)] ans, money = dfs(0, 0) print(ans, -money) ```
instruction
0
29,960
10
59,920
No
output
1
29,960
10
59,921
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Submitted Solution: ``` from operator import itemgetter def compute(prices, max_sum_having_coin): """ DP t?????????????????????????????????????????????????????£???????????????t??¨?????¶??? ????????§??????????????\??????????????? ??????t???????????????500?????????????????°???ct???????????\????????¬??¨???????¨????st??§?????????????????????????????????????????§ ???????????§???????????£????????????????°?????????????????????¨??????????????????????????????????????????i(i < t)??§??? 500?????????????????°???ci, ????????\????????¬??¨???????¨????si??§????????¨????????¨??????????????§???????????£????????????????????¶?????¨???????????????????????§????°???§????????? ??????????°?????????????????????????????????¨?????????????????????t??????????????????????°?????????????????????????????????¨???????????¨?????????????????? """ n = len(prices) m = max_sum_having_coin # table[i][j] = ??????i???????????¬??¨j???????????? (500??????????????§???, 500??????????????§?????¨????????¨??????????????£????????????????°????) table = [] for i in range(n + 1): table.append([None] * (m + 1)) table[0][0] = (0, 0) for i in range(1, n + 1): for j in range(0, m + 1): price = prices[i - 1] states = [] # ?????\???500?????????????????\???????????¨??? if price % 1000 != 0 and j >= 500 - price % 500: temp = table[i - 1][j - (500 - price % 500)] if temp != None: states.append((temp[0], temp[1] + price)) # ?????\???????????¨??? temp = table[i - 1][j] if temp != None: states.append(temp) # ?????\???500?????????????????\????????¨??? if price % 1000 == 0: if j + 500 <= m: temp = table[i - 1][j + 500] if temp != None: states.append((temp[0] + 1, temp[1] + price)) elif price % 1000 <= 500: temp = table[i - 1][j - (500 - price % 500)] if temp != None: states.append((temp[0] + 1, temp[1] + price)) elif j + price % 500 <= m: temp = table[i - 1][j + price % 500] if temp != None: states.append((temp[0] + 1, temp[1] + price)) if len(states) > 0: states.sort(key=itemgetter(1)) states.sort(key=itemgetter(0), reverse=True) table[i][j] = states[0] max_state = None for j in range(0, m + 1): state = table[n][j] if state == None: continue if max_state == None or state[0] > max_state[0] or (state[0] == max_state[0] and state[1] < max_state[1]): max_state = state return max_state while True: n = int(input()) if n == 0: break prices = [] for i in range(n): prices.append(int(input())) # ???????????????????????????500????????\????????????????????????????¨??????????500??????????¢????????????? # ????????°????????????n????????§?????????????????????????¨????????????? 500 * n ??§?????? max_sum_having_coin = 500 * n result = compute(prices, max_sum_having_coin) print(result[0], result[1]) ```
instruction
0
29,961
10
59,922
No
output
1
29,961
10
59,923
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Submitted Solution: ``` def compute_max_num_500_coins(prices): num_500_coins = 0 coin = 0 possible_thousand_coin_prices = [] for p in prices: if p % 1000 == 0: if coin >= 500: possible_thousand_coin_prices.append(p) elif p % 1000 <= 500: num_500_coins += 1 coin += 500 - p % 500 elif p % 500 <= coin: num_500_coins += 1 coin -= p % 500 else: coin += 500 - p % 500 thousand_coin_price = 0 if len(possible_thousand_coin_prices) > 0 and coin >= 500: thousand_coin_price = min(possible_thousand_coin_prices) num_500_coins += 1 coin -= 500 return num_500_coins, coin, thousand_coin_price def find_unneeded_prices(prices, remaining_coin): table = [] for i in range(len(prices) + 1): table.append([0] * (remaining_coin + 1)) for i in range(1, len(prices) + 1): p = prices[i - 1] prev_row = table[i - 1] row = table[i] for j in range(remaining_coin + 1): r = 500 - p % 500 if j < r: row[j] = prev_row[j] else: row[j] = max(prev_row[j], prev_row[j - r] + p) i = len(prices) j = remaining_coin unneeded_prices = [] while i > 0 and j > 0: if table[i][j] > table[i - 1][j]: j -= (500 - prices[i - 1] % 500) unneeded_prices.append(prices[i - 1]) i -= 1 return unneeded_prices while True: n = int(input()) if n == 0: break prices = [] for i in range(n): prices.append(int(input())) no_thousand_prices = [p for p in prices if p % 1000 != 0] max_num_500_coins, remaining_coin, thousand_coin_price = compute_max_num_500_coins(prices) target_prices = [p for p in prices if p % 1000 > 500] unneeded_prices = find_unneeded_prices(target_prices, remaining_coin) expense = sum(no_thousand_prices) - sum(unneeded_prices) + thousand_coin_price print(max_num_500_coins, expense) ```
instruction
0
29,962
10
59,924
No
output
1
29,962
10
59,925
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. 500-yen Saving "500-yen Saving" is one of Japanese famous methods to save money. The method is quite simple; whenever you receive a 500-yen coin in your change of shopping, put the coin to your 500-yen saving box. Typically, you will find more than one million yen in your saving box in ten years. Some Japanese people are addicted to the 500-yen saving. They try their best to collect 500-yen coins efficiently by using 1000-yen bills and some coins effectively in their purchasing. For example, you will give 1320 yen (one 1000-yen bill, three 100-yen coins and two 10-yen coins) to pay 817 yen, to receive one 500-yen coin (and three 1-yen coins) in the change. A friend of yours is one of these 500-yen saving addicts. He is planning a sightseeing trip and wants to visit a number of souvenir shops along his way. He will visit souvenir shops one by one according to the trip plan. Every souvenir shop sells only one kind of souvenir goods, and he has the complete list of their prices. He wants to collect as many 500-yen coins as possible through buying at most one souvenir from a shop. On his departure, he will start with sufficiently many 1000-yen bills and no coins at all. The order of shops to visit cannot be changed. As far as he can collect the same number of 500-yen coins, he wants to cut his expenses as much as possible. Let's say that he is visiting shops with their souvenir prices of 800 yen, 700 yen, 1600 yen, and 600 yen, in this order. He can collect at most two 500-yen coins spending 2900 yen, the least expenses to collect two 500-yen coins, in this case. After skipping the first shop, the way of spending 700-yen at the second shop is by handing over a 1000-yen bill and receiving three 100-yen coins. In the next shop, handing over one of these 100-yen coins and two 1000-yen bills for buying a 1600-yen souvenir will make him receive one 500-yen coin. In almost the same way, he can obtain another 500-yen coin at the last shop. He can also collect two 500-yen coins buying at the first shop, but his total expenditure will be at least 3000 yen because he needs to buy both the 1600-yen and 600-yen souvenirs in this case. You are asked to make a program to help his collecting 500-yen coins during the trip. Receiving souvenirs' prices listed in the order of visiting the shops, your program is to find the maximum number of 500-yen coins that he can collect during his trip, and the minimum expenses needed for that number of 500-yen coins. For shopping, he can use an arbitrary number of 1-yen, 5-yen, 10-yen, 50-yen, and 100-yen coins he has, and arbitrarily many 1000-yen bills. The shop always returns the exact change, i.e., the difference between the amount he hands over and the price of the souvenir. The shop has sufficient stock of coins and the change is always composed of the smallest possible number of 1-yen, 5-yen, 10-yen, 50-yen, 100-yen, and 500-yen coins and 1000-yen bills. He may use more money than the price of the souvenir, even if he can put the exact money, to obtain desired coins as change; buying a souvenir of 1000 yen, he can hand over one 1000-yen bill and five 100-yen coins and receive a 500-yen coin. Note that using too many coins does no good; handing over ten 100-yen coins and a 1000-yen bill for a souvenir of 1000 yen, he will receive a 1000-yen bill as the change, not two 500-yen coins. Input The input consists of at most 50 datasets, each in the following format. > n > p1 > ... > pn > n is the number of souvenir shops, which is a positive integer not greater than 100. pi is the price of the souvenir of the i-th souvenir shop. pi is a positive integer not greater than 5000. The end of the input is indicated by a line with a single zero. Output For each dataset, print a line containing two integers c and s separated by a space. Here, c is the maximum number of 500-yen coins that he can get during his trip, and s is the minimum expenses that he need to pay to get c 500-yen coins. Sample Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output for the Sample Input 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Example Input 4 800 700 1600 600 4 300 700 1600 600 4 300 700 1600 650 3 1000 2000 500 3 250 250 1000 4 1251 667 876 299 0 Output 2 2900 3 2500 3 3250 1 500 3 1500 3 2217 Submitted Solution: ``` import sys if sys.version[0] == '2': range, input = xrange, raw_input sys.setrecursionlimit(10 ** 6) MAX_COINS = 500 * 100 INF = 10 ** 9 def dfs(idx, coins): if idx == N: return 0, 0 elif dp[idx][coins] != -1: return dp[idx][coins] ret = (0, -INF) # not buy ret = max(ret, dfs(idx + 1, coins)) # buy using only bills change = (1000 - P[idx] % 1000) % 1000 cand, money = dfs(idx + 1, coins + change % 500) ret = max(ret, (cand + (change >= 500), money - P[idx])) # buy using both and get 500 if (P[idx] + 500) % 1000 <= coins: cand, money = dfs(idx + 1, coins - (P[idx] + 500) % 1000) ret = max(ret, (cand + 1, money - P[idx])) dp[idx][coins] = ret # print(idx, coins, ret) return ret while True: N = int(input()) if not N: break P = [int(input()) for _ in range(N)] dp = [[-1] * (MAX_COINS + 1) for _ in range(N + 1)] ans, money = dfs(0, 0) print(ans, -money) ```
instruction
0
29,963
10
59,926
No
output
1
29,963
10
59,927
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,398
10
60,796
Tags: binary search, greedy Correct Solution: ``` def readn(): return map(int, input().split()) n,m,a=readn()#map(int,input().split()) b,p=sorted(map(int,input().split()))[-min(n,m):],sorted(map(int,input().split())) r=min(n,m) mm=r l=0 while l<=r: mid=l+(r-l)//2 pri=sum([max(0,p[i]-b[mm-mid+i]) for i in range(mid)]) if pri<=a: l=mid+1 else: r=mid-1 print(r,max(0,sum(p[:r])-a)) ```
output
1
30,398
10
60,797
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,399
10
60,798
Tags: binary search, greedy Correct Solution: ``` import sys input = sys.stdin.readline def check(val,money): j = 0 for i in range(n-val,n): if b[i] >= p[j]: j += 1 continue diff = p[j]-b[i] if money-diff < 0: return 0 money -= diff j += 1 return 1 for _ in range(1): n,m,a = map(int,input().split()) b = list(map(int,input().split())) p = list(map(int, input().split())) b.sort() p.sort() low = 0 high = min(n,m) r = 0 while low <= high: mid = low+((high-low)//2) if check(mid,a): r = mid low = mid+1 else: high = mid-1 val = r cost = max(sum(p[0:val])-a,0) print(val,cost) ```
output
1
30,399
10
60,799
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,400
10
60,800
Tags: binary search, greedy Correct Solution: ``` n, m, a = map(int, input().split()) b, p = sorted(map(int, input().split())), sorted(map(int, input().split())) def f(k): return sum(max(0, p[i] - b[n - k + i]) for i in range(k)) def g(k): return sum(min(b[n - k + i], p[i]) for i in range(k)) x, y, d = 0, min(n, m) + 1, a while y - x > 1: k = (x + y) // 2 s = f(k) if s > a: y = k else: x, d = k, s print(x, max(0, g(x) - a + d)) ```
output
1
30,400
10
60,801
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,401
10
60,802
Tags: binary search, greedy Correct Solution: ``` import math # import collections # from itertools import permutations # from itertools import combinations # import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') '''def is_prime(n): j=2 while j*j<=n: if n%j==0: return 0 j+=1 return 1''' '''def gcd(x, y): while(y): x, y = y, x % y return x''' '''def lcm(x , y): return x*y//math.gcd(x,y)''' def check(n, mid , bike , price , a): need=0 for i in range(mid): if bike[n-mid+i]<price[i]: need+=price[i]-bike[n-mid+i] return need<=a def prob(): # n = int(input()) # s=input() # n,k=map(int, input().split()) n,m,a=map(int, input().split()) bike=list(map(int, input().split())) price=list(map(int, input().split())) bike.sort() price.sort() l=0 r=min(n,m) while l<=r: mid = (l+r)//2 if check(n,mid,bike,price,a): l=mid+1 else: r=mid-1 print(r,max(0,sum(price[:r])-a)) t=1 # t=int(input()) for _ in range(0,t): prob() ```
output
1
30,401
10
60,803
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,402
10
60,804
Tags: binary search, greedy Correct Solution: ``` # author : Tapan Goyal # MNIT Jaipur import math import bisect import itertools import sys I=lambda : sys.stdin.readline() one=lambda : int(I()) more=lambda : map(int,I().split()) linput=lambda : list(more()) mod=10**9 +7 inf=10**18 + 1 '''fact=[1]*100001 ifact=[1]*100001 for i in range(1,100001): fact[i]=((fact[i-1])*i)%mod ifact[i]=((ifact[i-1])*pow(i,mod-2,mod))%mod def ncr(n,r): return (((fact[n]*ifact[n-r])%mod)*ifact[r])%mod def npr(n,r): return (((fact[n]*ifact[n-r])%mod)) ''' def merge(a,b): i=0;j=0 c=0 ans=[] while i<len(a) and j<len(b): if a[i]<b[j]: ans.append(a[i]) i+=1 else: ans.append(b[j]) c+=len(a)-i j+=1 ans+=a[i:] ans+=b[j:] return ans,c def mergesort(a): if len(a)==1: return a,0 mid=len(a)//2 left,left_inversion=mergesort(a[:mid]) right,right_inversion=mergesort(a[mid:]) m,c=merge(left,right) c+=(left_inversion+right_inversion) return m,c def is_prime(num): if num == 1: return False if num == 2: return True if num == 3: return True if num%2 == 0: return False if num%3 == 0: return False t = 5 a = 2 while t <= int(math.sqrt(num)): if num%t == 0: return False t += a a = 6 - a return True def ceil(a,b): return (a+b-1)//b #///////////////////////////////////////////////////////////////////////////////////////////////// if __name__ == "__main__": n,m,k=more() mon=linput() bike=linput() mon.sort() bike.sort() low=0 high=min(n,m) def check(mid,k): for j in range(mid): k-=max(-mon[-j-1]+bike[mid-j-1],0) if k>=0: return k+1 return 0 while low<high: mid=(low+high + 1)//2 if check(mid,k): low=mid else: high=mid-1 kharch=sum(bike[:low]) print(low, max(kharch - k,0)) ```
output
1
30,402
10
60,805
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,403
10
60,806
Tags: binary search, greedy Correct Solution: ``` #!/usr/bin/python3 def readn(): return map(int, input().split()) n, m, a = readn() n = min(n, m) m = n b = sorted(readn())[-n:] p = sorted(readn()) r = 0 while r < n: t = (r+1 + n)//2 a1 = sum([max(0, p[i]-b[m+i-t]) for i in range(t)]) if a1 <= a: r = t else: n = t-1 print(r, max(0, sum(p[:r])-a)) ```
output
1
30,403
10
60,807
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,404
10
60,808
Tags: binary search, greedy Correct Solution: ``` # Legends Always Come Up with Solution # Author: Manvir Singh import os from io import BytesIO, IOBase import sys from collections import defaultdict, deque, Counter from math import sqrt, pi, ceil, log, inf, gcd, floor from itertools import combinations from bisect import * def check(b,p,a,mid): y,z=0,a for i in range(mid-1,-1,-1): y+=p[i] a-=max(0,p[i]-b[i-mid]) if a<0: return 0 return mid,max(0,y-z) def main(): n, m, a = map(int, input().split()) b = sorted(map(int, input().split())) p = sorted(map(int, input().split())) lo, hi, z = 0, min(n, m), 0 while lo <= hi: mid = (lo + hi) // 2 x = check(b, p, a, mid) if x: z = x lo = mid + 1 else: hi = mid - 1 print(*z) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() ```
output
1
30,404
10
60,809
Provide tags and a correct Python 3 solution for this coding contest problem. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
instruction
0
30,405
10
60,810
Tags: binary search, greedy Correct Solution: ``` K = int(input().split()[2]) A = list(map(int, input().split())) #[8, 1, 1, 2] B = list(map(int, input().split())) #[6, 3, 7, 5, 2] def canMake(partA, partB, K): for a, b in zip(partA, partB): if a >= b: pass else: K -= b - a if K < 0: return -1 return K def paramSearch(A, B, K): L = 0 R = min(len(A), len(B)) + 1 while L < R: M = int(L + (R - L) / 2); if canMake(A[-M:], B[:M], K) >= 0: L = M + 1 else: R = M return L A.sort(); B.sort(); #if __name__ == "__main__": bike = paramSearch(A, B, K) - 1; print(bike, end=' ') print(0 if sum(B[:bike]) - K < 0 else sum(B[:bike]) - K) ```
output
1
30,405
10
60,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') INF = 10 ** 18 MOD = 10**9+7 Ri = lambda : [int(x) for x in sys.stdin.readline().split()] ri = lambda : sys.stdin.readline().strip() n,m,left = Ri() a = Ri() b = Ri() a.sort() b.sort() def solve(no): if no > m: return -1 tleft = left mo = 0 i,j = n-no,0 while i < n: if a[i] >= b[j]: mo+=b[j] else: tleft-=(b[j]-a[i]) if tleft < 0 : return -1 mo+=a[i] i+=1;j+=1 # print(mo, tleft, no) return max(0, mo-tleft) l,r = 0,n ans = 10**18 totno = 0 while l <= r: mid = (l+r)//2 temp = solve(mid) if temp != -1: totno = max(totno, mid) ans = temp l = mid+1 else: r = mid-1 print(totno, ans) ```
instruction
0
30,406
10
60,812
Yes
output
1
30,406
10
60,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` #!/usr/bin/env python3 n, m, a = map(int, input().split()) b = sorted(map(int, input().split())) p = sorted(map(int, input().split())) left = 1 right = min(n, m) while left <= right: mid = (left + right) // 2 if a >= sum(max(0, x - y) for x, y in zip(p[:mid], b[-mid:])): left = mid + 1 else: right = mid - 1 print(right, max(0, sum(p[:right]) - a)) ```
instruction
0
30,407
10
60,814
Yes
output
1
30,407
10
60,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def check(mid,b,p,a): if mid > len(p): return 0 tot = 0 a1 = a for i in range(1,mid+1): tot += p[mid-i] a -= max(0,p[mid-i]-b[-i]) if a < 0: return 0 return mid,max(0,tot-a1) def main(): n,m,a = map(int,input().split()) b = sorted(list(map(int,input().split()))) p = sorted(list(map(int,input().split()))) hi,lo,mini = n,1,(0,0) while hi >= lo: mid = (hi+lo)//2 x = check(mid,b,p,a) if x: mini = x lo = mid+1 else: hi = mid-1 print(*mini) #Fast IO Region 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") if __name__ == '__main__': main() ```
instruction
0
30,408
10
60,816
Yes
output
1
30,408
10
60,817
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') 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") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def possible(x): b=boys[-x:] m=money[:x] need=0 for i in range(x): need+=max(0,m[i]-b[i]) return need<=a def maxBoys(): low=0 high=min(m,n) ans=0 while(low<=high): mid=low+(high-low)//2 if(possible(mid)): low=mid+1 ans=mid else: high=mid-1 return ans n,m,a=value() boys=sorted(array()) money=sorted(array())[:n] selected=maxBoys() ans=0 boys=boys[-selected:] money=money[:selected] # print(boys) # print(money) # print(selected) print(selected,max(0,sum(money)-a)) ```
instruction
0
30,409
10
60,818
Yes
output
1
30,409
10
60,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` n,m,a=map(int,input().split()) b,p=sorted(map(int,input().split())),sorted(map(int,input().split())) l=0 r=min(n,m) ans=0 mm=r #print(b,p) while l<r: mid=(l+r+1)//2 pri=sum([max(0,p[i]-b[mm-mid+i]) for i in range(mid)]) # print(pri,mid,l,r) if pri<=a: l=mid else: r=mid-1 # ans=max(mid,ans) print(l,max(0,sum(p[:l])-a)) ```
instruction
0
30,410
10
60,820
No
output
1
30,410
10
60,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` ''' Created on Nov 11, 2013 @author: Ismael ''' import sys def solve(): global sharedWallet global wallets global prices wallets.sort() prices.sort() size = min(len(wallets),len(prices)) wallets = wallets[len(wallets)-size:len(wallets)] prices = prices[:size] diffTot = sharedWallet + 1 l = 0 bestNb = 0 while(l < size-bestNb and diffTot > sharedWallet): diffTot = 0 totPrice = 0 nb = 0 i = 0 while(i+l<size): if(prices[i] > wallets[i]): diffTot += prices[i]-wallets[i+l] if(diffTot > sharedWallet): break totPrice += prices[i] nb += 1 if(nb > bestNb): bestNb = nb i += 1 l += 1 #print(totPrice) return (bestNb,max(0,totPrice-sharedWallet)) def main(): global sharedWallet global wallets global prices f = sys.stdin #f = open("input10.txt") (n,m,sharedWallet) = map(int,f.readline().split()) wallets = list(map(int,f.readline().split())) prices = list(map(int,f.readline().split())) #print(sharedWallet,wallets,prices) res = solve() print(res[0],res[1]) main() ```
instruction
0
30,411
10
60,822
No
output
1
30,411
10
60,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` ''' Created on Nov 11, 2013 @author: Ismael ''' import sys def solve(): wallets.sort() prices.sort() s = sharedWallet + 1 while(s > sharedWallet): s = 0 for i in range(min(len(wallets),len(prices))): s += prices[i]-wallets[i] if(s > sharedWallet): break if(s > sharedWallet): del wallets[0] return (len(wallets),s+sum(wallets)-sharedWallet) def main(): global sharedWallet global wallets global prices f = sys.stdin #f = open("input1.txt") (n,m,sharedWallet) = map(int,f.readline().split()) wallets = list(map(int,f.readline().split())) prices = list(map(int,f.readline().split())) #print(sharedWallet,wallets,prices) res = solve() print(res[0],res[1]) main() ```
instruction
0
30,412
10
60,824
No
output
1
30,412
10
60,825
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles. In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike. Output Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0. Examples Input 2 2 10 5 5 7 6 Output 2 3 Input 4 5 2 8 1 1 2 6 3 7 5 2 Output 3 8 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. Submitted Solution: ``` ''' Created on Nov 11, 2013 @author: Ismael ''' import sys def solve(): wallets.sort() prices.sort() s = sharedWallet + 1 while(s > sharedWallet): s = 0 for i in range(min(len(wallets),len(prices))): s += prices[i]-wallets[i] if(s > sharedWallet): break if(s > sharedWallet): del wallets[0] return (len(wallets),s+sum(wallets)-sharedWallet) def main(): global sharedWallet global wallets global prices f = sys.stdin #f = open("input1.txt") (n,m,sharedWallet) = map(int,f.readline().split()) wallets = list(map(int,f.readline().split())) prices = list(map(int,f.readline().split())) #print(sharedWallet,wallets,prices) res = solve() print(res) main() ```
instruction
0
30,413
10
60,826
No
output
1
30,413
10
60,827
Provide tags and a correct Python 3 solution for this coding contest problem. Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products. There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that: * the cashier needs 5 seconds to scan one item; * after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has. Output Print a single integer — the minimum number of seconds Vasya needs to get to the cashier. Examples Input 1 1 1 Output 20 Input 4 1 4 3 2 100 1 2 2 3 1 9 1 7 8 Output 100 Note In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
instruction
0
30,430
10
60,860
Tags: implementation Correct Solution: ``` n = int(input()) k = [int(x) for x in input().split()] ans = 10 ** 10 for i in range(n): ans = min(ans, sum(int(x) for x in input().split()) * 5 + k[i] * 15) print(ans) ```
output
1
30,430
10
60,861
Provide tags and a correct Python 3 solution for this coding contest problem. Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products. There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that: * the cashier needs 5 seconds to scan one item; * after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has. Output Print a single integer — the minimum number of seconds Vasya needs to get to the cashier. Examples Input 1 1 1 Output 20 Input 4 1 4 3 2 100 1 2 2 3 1 9 1 7 8 Output 100 Note In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
instruction
0
30,431
10
60,862
Tags: implementation Correct Solution: ``` n=int(input()) k=input() l=[] mini=1e9 for i in range(n): l.append(list(map(int,input().split(' ')))) # print(sum(l[i])*5+len(l[i])*15) mini=min(mini,sum(l[i])*5+len(l[i])*15) print(mini) ```
output
1
30,431
10
60,863
Provide tags and a correct Python 3 solution for this coding contest problem. Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products. There are n cashiers at the exit from the supermarket. At the moment the queue for the i-th cashier already has ki people. The j-th person standing in the queue to the i-th cashier has mi, j items in the basket. Vasya knows that: * the cashier needs 5 seconds to scan one item; * after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers. Input The first line contains integer n (1 ≤ n ≤ 100) — the number of cashes in the shop. The second line contains n space-separated integers: k1, k2, ..., kn (1 ≤ ki ≤ 100), where ki is the number of people in the queue to the i-th cashier. The i-th of the next n lines contains ki space-separated integers: mi, 1, mi, 2, ..., mi, ki (1 ≤ mi, j ≤ 100) — the number of products the j-th person in the queue for the i-th cash has. Output Print a single integer — the minimum number of seconds Vasya needs to get to the cashier. Examples Input 1 1 1 Output 20 Input 4 1 4 3 2 100 1 2 2 3 1 9 1 7 8 Output 100 Note In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5 + 15 = 515 seconds. But if he chooses the second queue, he will need 1·5 + 2·5 + 2·5 + 3·5 + 4·15 = 100 seconds. He will need 1·5 + 9·5 + 1·5 + 3·15 = 100 seconds for the third one and 7·5 + 8·5 + 2·15 = 105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
instruction
0
30,432
10
60,864
Tags: implementation Correct Solution: ``` n=int(input()) k=[] k=input().split() for i in range(0,n): k[i]=int(k[i]) m=[] for i in range(0,n): m.append([int(j)for j in input().split()]) t=[] for i in range(0,n): x=k[i] time=0 for j in range(0,x): time=time+(m[i][j]*5) time=time+(k[i]*15) t.append(time) print(min(t)) ```
output
1
30,432
10
60,865