description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
def r(): return list(map(int, input().split())) n, ans = 0, 0 a, b, c = r() m = int(input()) tt = [] for i in range(m): val, port = input().split() val = int(val) if port[0] is "U": tt.append([val, 1]) else: tt.append([val, 0]) tt.sort(key=lambda x: x[0]) tt2 = [] for x, y in tt: if y == 0 and b > 0: b -= 1 ans += x n += 1 elif y == 1 and a > 0: a -= 1 ans += x n += 1 else: tt2.append(x) mn = min(c, len(tt2)) ans += sum(tt2[:mn]) n += mn print(n, ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) m = int(input()) u = [] p = [] x = 0 y = 0 for i in range(m): s = input().split() if s[1] == "USB": u.append(int(s[0])) else: p.append(int(s[0])) u.sort() p.sort() u.reverse() p.reverse() while u and a: a -= 1 y += u[-1] u.pop() x += 1 while p and b: b -= 1 y += p[-1] p.pop() x += 1 u += p u.sort() u.reverse() while u and c: c -= 1 y += u[-1] u.pop() x += 1 print(x, y)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
USB = [] PS2 = [] u, p, b = map(int, input().split()) m = int(input()) for _ in range(m): a, s = input().split() if s[0] == "U": USB += [int(a)] else: PS2 += [int(a)] USB.sort() PS2.sort() ans = 0 n1 = min(u, len(USB)) n2 = min(p, len(PS2)) both = sorted(USB[n1:] + PS2[n2:]) print(n1 + n2 + min(len(both), b), sum(both[:b]) + sum(USB[:n1]) + sum(PS2[:n2]))
ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING VAR LIST FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) n = int(input()) u, p = [], [] for i in range(n): v, t = input().split() v = int(v) if t[0] == "U": u.append(v) else: p.append(v) u = sorted(u) p = sorted(p) s = 0 mod = [] k = 0 if len(u) < a: s += sum(u) k += len(u) else: s += sum(u[:a]) mod += u[a:] k += a if len(p) < b: s += sum(p) k += len(p) else: s += sum(p[:b]) mod += p[b:] k += b if len(mod) < c: s += sum(mod) k += len(mod) print(k, s) else: mod = sorted(mod) s += sum(mod[:c]) k += c print(k, s)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
import sys a, b, c = map(int, next(sys.stdin).split()) mouses = {"USB": [], "PS/2": []} for _ in range(int(input())): value, key = next(sys.stdin).strip().split() mouses[key].append(int(value)) for key in mouses.keys(): mouses[key].sort() usb, ps = min(a, len(mouses["USB"])), min(b, len(mouses["PS/2"])) remains = list(sorted(mouses["USB"][usb:] + mouses["PS/2"][ps:])) both = min(c, len(remains)) cost = sum(mouses["USB"][:usb]) + sum(mouses["PS/2"][:ps]) + sum(remains[:both]) print(usb + ps + both, cost)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR STRING VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
u, p, b = map(int, input().split()) t = int(input()) usb = [] ps2 = [] for i in range(t): val = input().split() if val[1] == "USB": usb.append(int(val[0])) else: ps2.append(int(val[0])) c = 0 s = 0 usb.sort(reverse=True) ps2.sort(reverse=True) leu = len(usb) lep = len(ps2) j = 0 for i in range(leu): if j >= u: break s += usb.pop() j += 1 c += 1 k = 0 for i in range(lep): if k >= p: break s += ps2.pop() k += 1 c += 1 for i in range(b): if len(usb) > 0 and len(ps2) > 0: if usb[-1] < ps2[-1]: s += usb.pop() c += 1 else: s += ps2.pop() c += 1 elif len(usb) > 0: s += usb.pop() c += 1 elif len(ps2) > 0: s += ps2.pop() c += 1 else: break print(c, s)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
from sys import stdin __author__ = "zihaozhu" USBOnly, UpsOnly, either = map(int, stdin.readline().split()) options = int(stdin.readline().rstrip()) usb = [] ups = [] for i in range(options): ip = stdin.readline().split() if ip[1] == "USB": usb.append(int(ip[0])) else: ups.append(int(ip[0])) usb.sort() ups.sort() numMouse = 0 cost = 0 usbCnt = 0 upsCnt = 0 while USBOnly > 0 and usbCnt < len(usb): cost += usb[usbCnt] usbCnt += 1 USBOnly -= 1 numMouse += 1 while UpsOnly > 0 and upsCnt < len(ups): cost += ups[upsCnt] upsCnt += 1 UpsOnly -= 1 numMouse += 1 while either > 0 and (usbCnt < len(usb) or upsCnt < len(ups)): either -= 1 val1 = float("inf") val2 = float("inf") if usbCnt < len(usb): val1 = usb[usbCnt] if upsCnt < len(ups): val2 = ups[upsCnt] numMouse += 1 minVal = min(val1, val2) if minVal == val1: usbCnt += 1 elif minVal == val2: upsCnt += 1 cost += min(val1, val2) print("%s %s" % (numMouse, cost))
ASSIGN VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = list(map(int, input().split())) M = int(input()) usb = [] ps = [] for i in range(0, M): t, m = input().split() t = int(t) if m[0] == "U": usb.append(t) else: ps.append(t) usb.sort() ps.sort() num = 0 cost = 0 hu = 0 hp = 0 tu = len(usb) tp = len(ps) def chku(): return hu < tu and (a > 0 or c > 0) def chkp(): return hp < tp and (b > 0 or c > 0) for i in range(0, M): f = chku() g = chkp() if f == 0 and g == 0: break elif g == 0: num += 1 cost += usb[hu] hu += 1 if a > 0: a -= 1 else: c -= 1 elif f == 0: num += 1 cost += ps[hp] hp += 1 if b > 0: b -= 1 else: c -= 1 else: num += 1 if usb[hu] < ps[hp]: if a > 0: a -= 1 else: c -= 1 hu += 1 cost += usb[hu - 1] else: if b > 0: b -= 1 else: c -= 1 hp += 1 cost += ps[hp - 1] print(num, " ", cost)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) m = int(input()) d = {"USB": [], "PS/2": []} for _ in range(m): v, t = input().split() d[t].append(int(v)) d["PS/2"].sort() d["USB"].sort() eq = cst = f1 = f2 = 0 nusb = len(d["USB"]) nps2 = len(d["PS/2"]) while a > 0 and f1 < nusb: a -= 1 eq += 1 cst += d["USB"][f1] f1 += 1 while b > 0 and f2 < nps2: b -= 1 eq += 1 cst += d["PS/2"][f2] f2 += 1 while c > 0 and (f1 < nusb or f2 < nps2): c -= 1 eq += 1 if f1 == nusb: cst += d["PS/2"][f2] f2 += 1 continue elif f2 == nps2: cst += d["USB"][f1] f1 += 1 continue if d["PS/2"][f2] < d["USB"][f1]: cst += d["PS/2"][f2] f2 += 1 else: cst += d["USB"][f1] f1 += 1 print(eq, cst)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR STRING VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR STRING VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR STRING VAR VAR NUMBER IF VAR VAR VAR VAR STRING VAR VAR NUMBER IF VAR STRING VAR VAR STRING VAR VAR VAR STRING VAR VAR NUMBER VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
u, p, b = tuple(map(int, input().split())) n = int(input()) equiped = 0 fullPrice = 0 arrusb = [] arrpc2 = [] for k in range(n): price, mtype = tuple(input().split()) if mtype == "USB": arrusb.append(int(price)) else: arrpc2.append(int(price)) arrusb.sort() arrpc2.sort() bcounter = 0 pcounter = 0 for i in range(n): if bcounter == len(arrusb): if p > 0: fullPrice += arrpc2[pcounter] p -= 1 equiped += 1 elif b > 0: fullPrice += arrpc2[pcounter] b -= 1 equiped += 1 pcounter += 1 continue if pcounter == len(arrpc2): if u > 0: fullPrice += arrusb[bcounter] u -= 1 equiped += 1 elif b > 0: fullPrice += arrusb[bcounter] b -= 1 equiped += 1 bcounter += 1 continue if arrusb[bcounter] < arrpc2[pcounter]: if u > 0: fullPrice += arrusb[bcounter] u -= 1 equiped += 1 elif b > 0: fullPrice += arrusb[bcounter] b -= 1 equiped += 1 bcounter += 1 else: if p > 0: fullPrice += arrpc2[pcounter] p -= 1 equiped += 1 elif b > 0: fullPrice += arrpc2[pcounter] b -= 1 equiped += 1 pcounter += 1 print(str(equiped) + " " + str(fullPrice))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
MAXINT = 10**10 a, b, c = map(int, input().split()) (m,) = map(int, input().split()) usb, ps2 = [], [] for _ in range(m): v, tp = input().split() if tp == "USB": usb.append(int(v)) else: ps2.append(int(v)) usb.sort() ps2.sort() ui, pi = 0, 0 un, pn = len(usb), len(ps2) usb.append(MAXINT) ps2.append(MAXINT) res, cost = 0, 0 while ui < un and a > 0: res += 1 cost += usb[ui] ui += 1 a -= 1 while pi < pn and b > 0: res += 1 cost += ps2[pi] pi += 1 b -= 1 while c > 0 and (ui < un or pi < pn): v = None if usb[ui] <= ps2[pi]: v = usb[ui] ui += 1 else: v = ps2[pi] pi += 1 res += 1 cost += v c -= 1 print(res, cost)
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NONE IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = list(map(int, input().split())) m = int(input()) usb = [] ps = [] cost = 0 for i in range(m): ca, t = list(input().split()) ca = int(ca) if t == "USB": usb.append(ca) else: ps.append(ca) usb.sort() ps.sort() usb_c = len(usb) ps_c = len(ps) j = 0 i = 0 ec = 0 while i < usb_c and a > 0: cost += usb[i] ec += 1 i += 1 a -= 1 while j < ps_c and b > 0: cost += ps[j] j += 1 ec += 1 b -= 1 na = usb[i:] + ps[j:] na.sort() i = 0 while i < len(na) and c > 0: cost = cost + na[i] ec += 1 i += 1 c -= 1 print(str(ec) + " " + str(cost))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
comp = [] ind = input().split() for i in range(3): comp.append(int(ind[i])) USB = [] PS2 = [] for i in range(int(input())): a = input().split() if a[1] == "PS/2": PS2.append(int(a[0])) else: USB.append(int(a[0])) USB.sort() PS2.sort() usb = min(comp[0], len(USB)) ps2 = min(comp[1], len(PS2)) ALL = USB[usb:] + PS2[ps2:] ALL.sort() two = min(len(ALL), comp[2]) print(usb + ps2 + two, sum(USB[:usb]) + sum(PS2[:ps2]) + sum(ALL[:two]))
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) m = int(input()) usb = [] ps2 = [] cost = 0 for _ in range(m): s = input().split() if s[1] == "USB": usb.append(int(s[0])) else: ps2.append(int(s[0])) usb.sort() ps2.sort() u = len(usb) p = len(ps2) x = min(a, u) y = min(b, p) t = x + y cost = sum(usb[:x]) + sum(ps2[:y]) if u > a or p > b: while c > 0 and (u > x or p > y): if x < u: if y < p: if usb[x] >= ps2[y]: cost += ps2[y] y += 1 else: cost += usb[x] x += 1 else: cost += usb[x] x += 1 else: cost += ps2[y] y += 1 t += 1 c -= 1 print(t, cost)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) m = int(input()) dct = {} if m == 0: print(0, 0) exit() for _ in range(m): i, j = input().split() if j in dct.keys(): dct[j].append(int(i)) else: dct[j] = [int(i)] lst1 = ["USB", "PS/2"] for i in lst1: if i not in dct.keys(): dct[i] = [] compct = 0 cost = 0 if len(dct["USB"]) <= a: cost += sum(list(dct["USB"])) compct += len(dct["USB"]) rem1 = [] else: lst = list(dct["USB"]) lst.sort() cost += sum(lst[:a]) compct += a rem1 = lst[a:] if len(dct["PS/2"]) <= b: cost += sum(list(dct["PS/2"])) compct += len(dct["PS/2"]) rem2 = [] else: lst = list(dct["PS/2"]) lst.sort() cost += sum(lst[:b]) compct += b rem2 = lst[b:] newlst = rem1 + rem2 l = len(newlst) if l <= c: compct += l cost += sum(newlst) else: newlst.sort() compct += c cost += sum(newlst[:c]) print(compct, cost)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR LIST STRING STRING FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR STRING VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR STRING VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING VAR FUNC_CALL VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
import sys a, b, c = map(int, sys.stdin.readline().split()) m = int(sys.stdin.readline()) num_item = 0 cost = 0 usb_l = [] ps2_l = [] for i in range(m): val, type_p = sys.stdin.readline().split() val = int(val) if type_p[0] == "U": usb_l.append(val) else: ps2_l.append(val) usb_l.sort() ps2_l.sort() len_usb = len(usb_l) len_ps2 = len(ps2_l) usb_l.append(float("inf")) ps2_l.append(float("inf")) pos1 = 0 pos2 = 0 if a > len_usb: num_item += len_usb cost += sum(usb_l[:-1]) pos1 = len_usb else: num_item += a cost += sum(usb_l[:a]) pos1 = a if b > len_ps2: num_item += len_ps2 cost += sum(ps2_l[:-1]) pos2 = len_ps2 else: num_item += b cost += sum(ps2_l[:b]) pos2 = b count = 0 while num_item < m and count < c: if usb_l[pos1] < ps2_l[pos2]: cost += usb_l[pos1] pos1 += 1 else: cost += ps2_l[pos2] pos2 += 1 num_item += 1 count += 1 print(num_item, cost)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
from sys import stdin, stdout usb, ps2, both = map(int, stdin.readline().split()) challengersu = [] challengersp = [] ind1 = 0 ind2 = 0 n = int(stdin.readline()) for i in range(n): price, s = stdin.readline().split() if s == "USB": challengersu.append(int(price)) else: challengersp.append(int(price)) ans, cnt = 0, 0 challengersu.sort() challengersp.sort() while usb and ind1 != len(challengersu) or ps2 and ind2 != len(challengersp): if usb and ps2 and ind1 != len(challengersu) and ind2 != len(challengersp): if usb and challengersu[ind1] <= challengersp[ind2]: ans += 1 cnt += challengersu[ind1] ind1 += 1 usb -= 1 elif ps2 and challengersu[ind1] >= challengersp[ind2]: ans += 1 cnt += challengersp[ind2] ind2 += 1 ps2 -= 1 elif usb and ind1 != len(challengersu): ans += 1 cnt += challengersu[ind1] ind1 += 1 usb -= 1 elif ps2 and ind2 != len(challengersp): ans += 1 cnt += challengersp[ind2] ind2 += 1 ps2 -= 1 while both and (ind1 != challengersu or ind2 != challengersp): both -= 1 if ind1 != len(challengersu) and ind2 != len(challengersp): if challengersu[ind1] < challengersp[ind2]: ans += 1 cnt += challengersu[ind1] ind1 += 1 else: ans += 1 cnt += challengersp[ind2] ind2 += 1 elif ind1 != len(challengersu): ans += 1 cnt += challengersu[ind1] ind1 += 1 elif ind2 != len(challengersp): ans += 1 cnt += challengersp[ind2] ind2 += 1 stdout.write(str(ans) + " " + str(cnt))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = list(map(int, input().split())) m = int(input()) USB = [] PS = [] i2 = 0 i1 = 0 ans1 = 0 ans2 = 0 for i in range(m): n, k = input().split() if k == "USB": USB.append(int(n)) else: PS.append(int(n)) USB.sort() PS.sort() for i in range(a): if i >= len(USB): break ans1 += USB[i] ans2 += 1 i1 = i + 1 for i in range(b): if i >= len(PS): break ans1 += PS[i] ans2 += 1 i2 = i + 1 for i in range(c): if i2 < len(PS) and i1 < len(USB): if PS[i2] < USB[i1]: ans1 += PS[i2] i2 += 1 ans2 += 1 else: ans1 += USB[i1] i1 += 1 ans2 += 1 elif i2 >= len(PS): if i1 < len(USB): ans1 += USB[i1] i1 += 1 ans2 += 1 elif i1 >= len(USB): if i2 < len(PS): ans1 += PS[i2] i2 += 1 ans2 += 1 print(ans2, ans1)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
z, b, c = map(int, input().split()) m = int(input()) a = [] for _ in range(m): x, y = input().split() if y == "USB": a.append([int(x), 1]) else: a.append([int(x), 2]) cost = ans = 0 a.sort() for i in a: if i[1] == 1: if z: z -= 1 ans += 1 cost += i[0] elif c: c -= 1 ans += 1 cost += i[0] elif b: b -= 1 ans += 1 cost += i[0] elif c: c -= 1 ans += 1 cost += i[0] print(ans, cost)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = input().split() a, b, c = int(a), int(b), int(c) usb = "USB" ps2 = "PS/2" u = [] ps = [] n = int(input()) while n > 0: cost, s = input().split() if s == usb: u.append(int(cost)) elif s == ps2: ps.append(int(cost)) n -= 1 u.sort() ps.sort() cost, count = 0, 0 a_curr, ps_curr = 0, 0 a_count, ps_count = len(u), len(ps) cost += sum(u[0 : min(a_count, a)]) a_curr = min(a_count, a) count = a_curr cost += sum(ps[0 : min(ps_count, b)]) ps_curr = min(ps_count, b) count += ps_curr new_arr = u[a_curr:] + ps[ps_curr:] count += min(c, len(new_arr)) new_arr.sort() cost += sum(new_arr[:c]) print(count, cost, sep=" ")
ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
from sys import stdin A, B, C = list(map(int, stdin.readline().split())) M = int(stdin.readline()) usb_mouses = [] ps2_mouses = [] for m in range(M): p, t = stdin.readline().split() if t == "USB": usb_mouses.append(int(p)) elif t == "PS/2": ps2_mouses.append(int(p)) usb_mouses.sort() ps2_mouses.sort() usb_cur = 0 ps2_cur = 0 cost = 0 while usb_cur < len(usb_mouses) and A > 0: cost += usb_mouses[usb_cur] usb_cur += 1 A -= 1 while ps2_cur < len(ps2_mouses) and B > 0: cost += ps2_mouses[ps2_cur] ps2_cur += 1 B -= 1 while C > 0: if usb_cur < len(usb_mouses) and ps2_cur < len(ps2_mouses): if usb_mouses[usb_cur] < ps2_mouses[ps2_cur]: cost += usb_mouses[usb_cur] usb_cur += 1 else: cost += ps2_mouses[ps2_cur] ps2_cur += 1 elif usb_cur < len(usb_mouses): cost += usb_mouses[usb_cur] usb_cur += 1 elif ps2_cur < len(ps2_mouses): cost += ps2_mouses[ps2_cur] ps2_cur += 1 else: break C -= 1 print("{} {}".format(usb_cur + ps2_cur, cost))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) m = int(input()) usb = [] ps = [] count = 0 for _ in range(m): x, y = input().split() if y == "USB": usb.append(int(x)) else: ps.append(int(x)) usb.sort() ps.sort() ans = 0 usbc = 0 psc = 0 if a > len(usb): ans += sum(usb) usbc = len(usb) count += len(usb) else: for i in range(a): ans += usb[i] usbc = i count += 1 if a > 0: usbc += 1 if b > len(ps): ans += sum(ps) psc = len(ps) count += len(ps) else: for i in range(b): ans += ps[i] psc = i count += 1 if b > 0: psc += 1 cc = c if len(usb) - 1 >= usbc and len(ps) - 1 >= psc: while usbc <= len(usb) - 1 and psc <= len(ps) - 1 and cc > 0: if usb[usbc] < ps[psc]: ans += usb[usbc] usbc += 1 cc -= 1 count += 1 else: ans += ps[psc] psc += 1 cc -= 1 count += 1 if cc > 0 and usbc <= len(usb) - 1: while cc > 0 and usbc <= len(usb) - 1: cc -= 1 ans += usb[usbc] usbc += 1 count += 1 if cc > 0 and psc <= len(ps) - 1: while cc > 0 and psc <= len(ps) - 1: cc -= 1 ans += ps[psc] psc += 1 count += 1 print(count, ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
c = list(map(int, input().split())) m = int(input()) a = [] for i in range(m): n, t = input().split() a.append((int(n), int(t == "PS/2"))) a.sort() res = 0 b = [0] * 3 for n, t in a: if c[t] - b[t] > 0: b[t] += 1 res += n elif c[2] - b[2] > 0: b[2] += 1 res += n print(sum(b), res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = (int(i) for i in input().split()) m = int(input()) l1 = [] l2 = [] c1, c2 = 0, 0 c3 = 0 s = 0 for i in range(m): x, y = (i for i in input().split()) if y == "USB": l1.append(int(x)) c1 += 1 else: l2.append(int(x)) c2 += 1 l1.sort() l2.sort() i1, i2 = 0, 0 while m > 0: if i1 < c1 and i2 < c2: if l1[i1] < l2[i2]: if a > 0: a -= 1 s += l1[i1] i1 += 1 c3 += 1 elif c > 0: c -= 1 s += l1[i1] i1 += 1 c3 += 1 else: i1 = c1 elif b > 0: b -= 1 s += l2[i2] i2 += 1 c3 += 1 elif c > 0: c -= 1 s += l2[i2] i2 += 1 c3 += 1 else: i2 = c2 elif i1 == c1 and i2 == c2: break elif i1 == c1: if b > 0: b -= 1 s += l2[i2] i2 += 1 c3 += 1 elif c > 0: c -= 1 s += l2[i2] i2 += 1 c3 += 1 else: i2 = c2 elif a > 0: a -= 1 s += l1[i1] i1 += 1 c3 += 1 elif c > 0: c -= 1 s += l1[i1] i1 += 1 c3 += 1 else: i1 = c1 m -= 1 print(c3, s)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) n = int(input()) mice_usb = [] mice_ps = [] for i in range(n): price, mice_type = input().split() if mice_type[0] == "U": mice_usb.append(int(price)) else: mice_ps.append(int(price)) usb = min(len(mice_usb), a) ps2 = min(len(mice_ps), b) mice_usb.sort() mice_ps.sort() answer = sum(mice_usb[:usb]) + sum(mice_ps[:ps2]) num = usb + ps2 left = mice_usb[usb:] + mice_ps[ps2:] left.sort() any_num = min(len(left), c) answer += sum(left[:any_num]) num += any_num print(num, answer)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = map(int, input().split()) m = int(input()) s1 = [list(map(str, input().split())) for i in range(m)] for x in s1: x[0] = int(x[0]) s1 = sorted(s1) k = 0 s = 0 for x in s1: flag = True if x[1] == "USB": if a > 0: flag = False k += 1 a -= 1 s += x[0] elif b > 0: flag = False k += 1 b -= 1 s += x[0] if flag: if c > 0: k += 1 c -= 1 s += x[0] print(k, s)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
def list_from_input(): return list(map(int, input().split())) class Mouse: def __init__(self, price, type): self.type = type self.price = price @classmethod def from_input(cls): mouse_data = input().split() price = int(mouse_data[0]) type = mouse_data[1][0] return Mouse(price, type) def main(): usb_pc, ps_pc, both_pc = list_from_input() mouses_count = int(input()) mouses = [] for i in range(mouses_count): mouses.append(Mouse.from_input()) mouses.sort(key=lambda mouse: mouse.price) purchase_amount = 0 pc_with_mouses = 0 for mouse in mouses: if mouse.type is "U" and usb_pc > 0: usb_pc -= 1 purchase_amount += mouse.price pc_with_mouses += 1 elif mouse.type is "P" and ps_pc > 0: ps_pc -= 1 purchase_amount += mouse.price pc_with_mouses += 1 elif both_pc > 0: both_pc -= 1 purchase_amount += mouse.price pc_with_mouses += 1 print(pc_with_mouses, purchase_amount) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
inputLine = input().split() pcU = int(inputLine[0]) pcP = int(inputLine[1]) pcUP = int(inputLine[2]) priceList1 = [] priceList2 = [] readSize = int(input()) for i in range(readSize): tmpList = input().split() if tmpList[1] == "USB": priceList1.append(int(tmpList[0])) else: priceList2.append(int(tmpList[0])) uNum = len(priceList1) pNum = len(priceList2) priceList1.sort() priceList2.sort() price = 0 equip = 0 tmp = min(uNum, pcU) price += sum(priceList1[0:tmp]) equip += tmp del priceList1[0:tmp] uNum -= tmp pcU -= tmp tmp = min(pNum, pcP) price += sum(priceList2[0:tmp]) equip += tmp del priceList2[0:tmp] pNum -= tmp pcP -= tmp priceList = priceList1 priceList.extend(priceList2) priceList.sort() fNum = len(priceList) tmp = min(fNum, pcUP) price += sum(priceList[0:tmp]) equip += tmp print(str(equip) + " " + str(price))
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
def calc(a, b, c, p): s = 0 comp = 0 for k, v in p: if a > 0 or b > 0 or c > 0: if "USB" == v and (a > 0 or c > 0): if a > 0: comp += 1 a -= 1 elif c > 0: comp += 1 c -= 1 s += int(k) elif "PS/2" == v and (b > 0 or c > 0): if b > 0: comp += 1 b -= 1 elif c > 0: comp += 1 c -= 1 s += int(k) else: return comp, s return comp, s def convertor(lis): a, b = lis.split(" ") return [int(a), b] a, b, c = list(map(int, input().split(" "))) m = int(input()) p = sorted([convertor(input()) for i in range(m)]) r = calc(a, b, c, p) print(str(r[0]) + " " + str(r[1]))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF STRING VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR IF STRING VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING RETURN LIST FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
from sys import stdin as fin a, b, c = [int(i) for i in input().split()] n = int(input()) usb = [] ps2 = [] for i in range(n): a1, a2 = fin.readline().strip().split() if a2 == "USB": usb.append(int(a1)) else: ps2.append(int(a1)) usb = sorted(usb) ps2 = sorted(ps2) n1 = len(usb) n2 = len(ps2) count = 0 cost = 0 l = 0 r = 0 cost += sum(usb[: min(a, n1)]) l += min(a, n1) count += l cost += sum(ps2[: min(b, n2)]) r += min(b, n2) count += r newarray = sorted(usb[l:] + ps2[r:]) cost += sum(newarray[: min(c, len(newarray))]) count += min(c, len(newarray)) print(count, cost)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = [int(i) for i in input().split()] m = int(input()) u_list = [] p_list = [] for i in range(m): price, port = input().split() price = int(price) if port[0] == "U": u_list.append(price) else: p_list.append(price) u_list_len = len(u_list) p_list_len = len(p_list) u_list = sorted(u_list) p_list = sorted(p_list) mouse_count = 0 cost = 0 if u_list_len >= a: mouse_count += a cost += sum(u_list[:a]) u_list = u_list[a:] else: mouse_count += u_list_len cost += sum(u_list) u_list = [] if p_list_len >= b: mouse_count += b cost += sum(p_list[:b]) p_list = p_list[b:] else: mouse_count += p_list_len cost += sum(p_list) p_list = [] pu_list = p_list + u_list pu_list = sorted(pu_list) pu_list_len = len(pu_list) if pu_list_len >= c: mouse_count += c cost += sum(pu_list[:c]) else: mouse_count += pu_list_len cost += sum(pu_list) print(mouse_count, cost)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Due to the increase in the number of students of Berland State University it was decided to equip a new computer room. You were given the task of buying mouses, and you have to spend as little as possible. After all, the country is in crisis! The computers bought for the room were different. Some of them had only USB ports, some — only PS/2 ports, and some had both options. You have found a price list of a certain computer shop. In it, for m mouses it is specified the cost and the type of the port that is required to plug the mouse in (USB or PS/2). Each mouse from the list can be bought at most once. You want to buy some set of mouses from the given price list in such a way so that you maximize the number of computers equipped with mouses (it is not guaranteed that you will be able to equip all of the computers), and in case of equality of this value you want to minimize the total cost of mouses you will buy. -----Input----- The first line contains three integers a, b and c (0 ≤ a, b, c ≤ 10^5)  — the number of computers that only have USB ports, the number of computers, that only have PS/2 ports, and the number of computers, that have both options, respectively. The next line contains one integer m (0 ≤ m ≤ 3·10^5)  — the number of mouses in the price list. The next m lines each describe another mouse. The i-th line contains first integer val_{i} (1 ≤ val_{i} ≤ 10^9)  — the cost of the i-th mouse, then the type of port (USB or PS/2) that is required to plug the mouse in. -----Output----- Output two integers separated by space — the number of equipped computers and the total cost of the mouses you will buy. -----Example----- Input 2 1 1 4 5 USB 6 PS/2 3 PS/2 7 PS/2 Output 3 14 -----Note----- In the first example you can buy the first three mouses. This way you will equip one of the computers that has only a USB port with a USB mouse, and the two PS/2 mouses you will plug into the computer with PS/2 port and the computer with both ports.
a, b, c = list(map(int, input().split())) n, u0, u1 = int(input()), [], [] for i in range(n): e0, e1 = input().split() if e1 == "USB": u0.append(int(e0)) else: u1.append(int(e0)) u0.sort() u1.sort() n = 0 x = 0 if len(u0) > a: x = x + sum(u0[:a]) u0 = u0[a:] n = n + a else: x = x + sum(u0) n = n + len(u0) u0 = [] if len(u1) > b: x = x + sum(u1[:b]) u1 = u1[b:] n = n + b else: x = x + sum(u1) n = n + len(u1) u1 = [] u = u1 + u0 u.sort() if len(u) > c: x = x + sum(u[:c]) n = n + c else: x = x + sum(u) n = n + len(u) print(str(n) + " " + str(x))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
N = int(input()) A = [int(a) for a in input().split()] X = [-1] * (N + 1) k = 2 while k <= N: X[k] = 1 for i in range(k * 2, N + 1, k): X[i] = 0 while k <= N and X[k] >= 0: k += 1 P = [i for i in range(N + 1) if X[i] == 1] s = sum(A) L = [] for p in P: if s % p == 0: L.append(p) if len(L) == 0: print(-1) else: mi = 1 << 100 for m in L: mm = m // 2 k = 0 ans = 0 for i in range(N): if A[i]: k = (k + 1) % m if k <= mm: ans += k elif k == mm and A[i]: pass else: ans += m - k mi = min(mi, ans) print(mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
PI = 3.141592653589793 INF = float("inf") MOD = 1000000007 def bin32(num): return "{0:032b}".format(num) def add(x, y): return (x + y) % MOD def sub(x, y): return (x - y + MOD) % MOD def mul(x, y): return x * y % MOD def gcd(x, y): if y == 0: return x return gcd(y, x % y) def lcm(x, y): return x * y // gcd(x, y) def power(x, y): res = 1 x %= MOD while y != 0: if y & 1: res = mul(res, x) y >>= 1 x = mul(x, x) return res def mod_inv(n): return power(n, MOD - 2) def prob(p, q): return mul(p, power(q, MOD - 2)) def ii(): return int(input()) def li(): return [int(i) for i in input().split()] def ls(): return [i for i in input().split()] n = ii() a = li() if sum(a) == 1: print(-1) exit(0) ind = -1 total = sum(a) fac = [] for i in range(2, n + 1): if total % i == 0: fac.append(i) store = [] for i in range(n): if a[i] == 1: store.append(i) n = len(store) final = INF for ind in fac: ans = 0 for i in range(0, n, ind): tmp = [] cnt = 0 for j in range(i, i + ind): tmp.append(store[j]) if ind % 2: med = ind // 2 for i in tmp: cnt += abs(i - tmp[med]) else: med1 = ind // 2 med2 = (ind - 1) // 2 ans1 = 0 ans2 = 0 for i in tmp: ans1 += abs(i - tmp[med1]) ans2 += abs(i - tmp[med2]) cnt += min(ans1, ans2) ans += cnt final = min(final, ans) print(final)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
import sys def getAllFactors(x): factors = [] for i in range(1, int(x**0.5) + 1): if x % i == 0: factors.append(i) if x // i != i: factors.append(x // i) return sorted(factors) def getPrimeFactors(x): primeFacs = [] for f in getAllFactors(x): if len(getAllFactors(f)) == 2: primeFacs.append(f) return primeFacs def main(): n = int(input()) a = readIntArr() total = sum(a) if total <= 1: print(-1) return ans = inf for factor in getPrimeFactors(total): tempans = 0 cnt = 0 indexes = [] for i, x in enumerate(a): if x == 1: indexes.append(i) cnt += 1 if cnt == factor: medianIndex = indexes[factor // 2] for j in indexes: tempans += abs(j - medianIndex) indexes = [] cnt = 0 ans = min(ans, tempans) print(ans) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
def count(x): ans = 0 for i in range(0, m, x): st = (2 * i + x - 1) // 2 for j in range(i, x + i): ans += abs(a[j] - a[st]) return ans n = int(input()) data = list(map(int, input().split())) a = [] m = 0 for i in range(n): if data[i] == 1: a.append(i) m += 1 k = [] for i in range(2, m + 1): if m % i == 0: k.append(i) l, r = 0, len(k) - 1 while r - l > 7: m1 = l + (r - l) // 3 m2 = r - (r - l) // 3 if count(k[m1]) > count(k[m2]): l = m1 else: r = m2 t = 10**18 for i in range(l, r + 1): t = min(t, count(k[i])) if t == 10**18: print(-1) else: print(t)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
def f(n): minus = n // 2 if n % 2 == 0 else 0 n //= 2 return n * (n + 1) - minus def cal(a, k): ans = 0 cur = 0 for i, x in enumerate(a): if x == 0: ans += min(cur, k - cur) else: cur += 1 return ans + f(k) def uoc(x): i = 1 arr = [] while i * i <= x: if x % i == 0: arr.append(i) if i * i != x: arr.append(x // i) i += 1 return sorted(arr)[1:] n = int(input()) a = list(map(int, input().split())) min_ = float("inf") for u in uoc(sum(a)): cur = 0 l = None s = 0 for i, x in enumerate(a): if x == 1: cur += 1 if l == None: l = i if cur == u: s += cal(a[l : i + 1], u) cur, l = 0, None min_ = min(min_, s) if min_ < float("inf"): print(min_) else: print(-1)
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NONE ASSIGN VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
ret = 0 p = [True] * 100500 for i in range(2, 100300): for j in range(i * i, 100300, i): p[j] = False a = [i for i in range(2, 100001) if p[i]] n = int(input()) b = list(map(int, input().split())) def f(x): sm = 0 ths = 0 chk = [] for i in range(n): if b[i]: sm += 1 chk.append(i) if sm == x: for j in chk: ths += abs(j - chk[x // 2]) chk = [] sm = 0 return ths ans = 10**18 s = sum(b) for x in a: if s % x == 0: ans = min(ans, f(x)) print(ans if ans != 10**18 else -1)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) x = [] c = 0 for i in range(n): if a[i]: x.append(i) c += 1 l = set() for i in range(1, int(c**0.5) + 1): if c % i == 0: l.add(i) l.add(c // i) ans = 10**18 for ll in l: if ll == 1: continue res = 0 for i in range(0, len(x), ll): xi = x[i : i + ll] score = 0 mid = xi[ll // 2] for j in range(len(xi)): score += abs(mid - xi[j]) res += score ans = min(ans, res) if ans == 10**18: print(-1) else: print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
from itertools import accumulate def divisors(n): tab = [] for i in range(2, int(n**0.5) + 2): if n % i == 0: while n % i == 0: n //= i tab.append(i) if n > 1: tab.append(n) return tab n = int(input()) a = list(map(int, input().split())) s = sum(a) tab = divisors(s) if s == 1: print(-1) else: m = 10**18 pref = list(accumulate(a)) for k in tab: x = 0 for i in range(n - 1): x += min(pref[i] % k, k - pref[i] % k) m = min(m, x) print(m)
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
n = int(input()) a = list(map(int, input().split())) ids = [] for i in range(n): if a[i] == 1: ids.append(i) m = len(ids) sum = m if sum == 1: print(-1) exit() def calculate(inc): ret = 0 for i in range(0, m, inc): mid = (i + (i + inc - 1)) // 2 for j in range(i, i + inc): ret += abs(ids[j] - ids[mid]) return ret ans = 10**18 div = 2 while div <= sum: if sum % div == 0: get = calculate(div) ans = min(ans, get) div += 1 ans = min(ans, calculate(m)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
def simple_div(x): if not x & 1: yield 2 while not x & 1: x >>= 1 i = 3 while i * i <= x: if x % i == 0: yield i while x % i == 0: x //= i i += 2 if x != 1: yield x def __main__(): n = int(input()) a = list(map(int, input().split())) sa = sum(a) a.pop() if sa == 1: print(-1) return res = 2**64 for d in simple_div(sa): tmp = 0 m = 0 for x in a: m = (x + m) % d tmp += m if m * 2 <= d else d - m if tmp < res: res = tmp print(res) __main__()
FUNC_DEF IF BIN_OP VAR NUMBER EXPR NUMBER WHILE BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER EXPR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
def prime(x): if count % x == 0: return True return False n = int(input()) a = list(map(int, input().split())) count = 0 b = [0] for i in range(n): if a[i] == 1: b.append(i + 1) count += 1 jump = 0 i, j, k, add, inc, dj, tera = 0, 0, 0, 0, 0, 0, 0 mini = 999999999999 s = 1 if count == 1: print(-1) else: while s <= count: i, j, k, add, inc, dj, tera = 0, 0, 0, 0, 0, 0, 0 while prime(s): if s == 1: jump = count else: jump = s i = j + 1 j += jump dj = j k = i + jump // 2 inc = 1 tera = 1 if i == count + 1: break while tera <= jump // 2: add += (abs(b[i] - b[k]) + abs(b[dj] - b[k])) * inc i += 1 dj -= 1 tera += 1 if add > 0: mini = min(mini, add) s += 1 print(mini)
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
n = int(input()) l = list(map(int, input().split())) s, pref = 0, [] for i in range(n): s += l[i] if l[i]: pref.append(i) if s == 1: print(-1) exit() divs = [s] for i in range(2, int(s**0.5) + 1): if s % i == 0: divs.extend([i, s // i]) ans, value = 10**18, -3 for i in divs: cnt = 0 for j in range(0, s, i): mid = (i + 2 * j - 1) // 2 for k in range(j, j + i): cnt += abs(pref[k] - pref[mid]) ans = min(ans, cnt) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
n = int(input()) a = list(map(int, input().split())) if sum(a) == 1: print(-1) return sm = sum(a) nmax = 10**5 + 10 eratos = [(0) for i in range(nmax + 1)] prime = [] cnt = 2 while True: while cnt <= nmax and eratos[cnt]: cnt += 1 if cnt > nmax: break eratos[cnt] = 1 prime.append(cnt) for i in range(cnt**2, nmax + 1, cnt): eratos[i] = 1 dvls = [] for i in prime: if sm % i == 0: dvls.append(i) ansls = [] ls = [] cnti = 0 for dv in dvls: ans = 0 if dv == 2: for i in range(n): if a[i]: cnti += 1 if cnti % 2: pivot = i else: ans += i - pivot else: for i in range(n): if a[i]: cnti += 1 if 1 <= cnti % dv <= dv // 2: ls.append(i) elif cnti % dv == dv // 2 + 1: pivot = i for j in ls: ans += pivot - j ls.clear() else: ans += i - pivot ansls.append(ans) print(min(ansls))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
n = int(input()) a = [int(x) for x in input().split()] m = 0 b = [] for i in range(n): m += a[i] if a[i] == 1: b.append(i) ans = n * m for i in range(2, m + 1): preans = 0 if m % i != 0: continue c = 0 for j in range(m): wg = j // i * i + i // 2 if j != wg: preans += abs(b[wg] - b[j]) ans = min(ans, preans) if m == 1: ans = -1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) S = sum(A) if S == 1: print(-1) exit() div_list = [S] for d in range(2, int(S**0.5) + 1): if S % d == 0: div_list.append(d) div_list.append(S // d) if len(div_list) > 2: if div_list[-1] == div_list[-2]: div_list.pop() ans_best = 10**10 for D in div_list: ans = 0 cnt = 0 i_list = [] for i, a in enumerate(A): if a == 1: i_list.append(i) cnt += 1 if cnt == D: cnt = 0 j = i_list[D // 2] for ii in i_list: ans += abs(ii - j) i_list = [] ans_best = min(ans_best, ans) print(ans_best) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version, $1 \le n \le 10^5$ and $0 \le a_i \le 1$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $n$ boxes of chocolate, numbered from $1$ to $n$. Initially, the $i$-th box contains $a_i$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $n$ empty boxes. In other words, at least one of $a_1, a_2, \ldots, a_n$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $k > 1$ such that the number of pieces in each box is divisible by $k$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $i$ and put it into either box $i-1$ or box $i+1$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the number of chocolate boxes. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 1$) — the number of chocolate pieces in the $i$-th box. It is guaranteed that at least one of $a_1, a_2, \ldots, a_n$ is positive. -----Output----- If there is no way for Charlie to make Alice happy, print $-1$. Otherwise, print a single integer $x$ — the minimum number of seconds for Charlie to help Bob make Alice happy. -----Examples----- Input 3 1 0 1 Output 2 Input 1 1 Output -1
import sys def prime_decomposition(n): i = 2 table = [] while i * i <= n: while n % i == 0: n //= i table.append(i) i += 1 if n > 1: table.append(n) return table input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) su = sum(A) if su == 1: print(-1) exit() primes = list(set(prime_decomposition(su))) ans = float("inf") Idx1 = [i for i, a in enumerate(A) if a] for p in primes: an = 0 half = p // 2 for t in zip(*([iter(Idx1)] * p)): idx = t[half] an += sum(abs(i - idx) for i in t) ans = min(ans, an) print(ans)
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP LIST FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
ssss = int(input()) for _ in range(ssss): n, t, a, b = map(int, input().split()) o = list(map(int, input().split())) s = list(map(int, input().split())) la = sum([(1) for i in o if i == 0]) lb = sum([(1) for i in o if i == 1]) ps = [(o[i], s[i]) for i in range(n)] d = {} cd = {} dla = {} dlb = {} sm = smt = 0 ans = 0 ag = {} for p in ps: if p[1] not in ag: ag[p[1]] = [] ag[p[1]].append(b if p[0] else a) ks = sorted(list(set([p[1] for p in ps]))) for k in ks: d[k - 1] = sm cd[k - 1] = smt dla[k - 1] = la dlb[k - 1] = lb sm += sum(ag[k]) smt += len(ag[k]) tmp = len([i for i in ag[k] if i == a]) la -= tmp lb -= len(ag[k]) - tmp ts = sorted(list(d.items()), key=lambda x: x[0]) ts.append((t, sm)) cd[t] = n ans = 0 for tss in ts: ft = tss[0] - tss[1] if ft >= 0: if tss[0] == t: ans = max(ans, n) break cans = cd[tss[0]] csa = min(ft // a, dla[tss[0]]) cans += csa ft -= csa * a csb = min(ft // b, dlb[tss[0]]) cans += csb ft -= csb * b ans = max(ans, cans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER LIST EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
num_tests = int(input()) for test in range(num_tests): inp = input().rstrip().split(" ") n = int(inp[0]) T = int(inp[1]) a = int(inp[2]) b = int(inp[3]) inp = input().rstrip().split(" ") difficulty = [int(inp[i]) for i in range(len(inp))] inp = input().rstrip().split(" ") mandatory_at = [int(inp[i]) for i in range(len(inp))] joint = list(zip(mandatory_at, difficulty)) joint.sort(key=lambda tup: tup[1]) joint.sort(key=lambda tup: tup[0]) num_hard = sum(difficulty) num_easy = n - num_hard scores = [] mandatory_score = 0 mandatory_time = 0 for time, difficulty in joint: left_time = time - 1 - mandatory_time if left_time >= 0: score = mandatory_score if int(left_time / a) <= num_easy: score += int(left_time / a) else: score += num_easy left_time -= num_easy * a score += min(int(left_time / b), num_hard) scores.append(score) else: scores.append(0) mandatory_time += difficulty * (b - a) + a mandatory_score += 1 num_easy = num_easy - (1 - difficulty) num_hard = num_hard - difficulty left_time = T - mandatory_time if left_time >= 0: score = mandatory_score if int(left_time / a) <= num_easy: score += int(left_time / a) else: score += num_easy left_time -= num_easy * a score += min(int(left_time / b), num_hard) scores.append(score) else: scores.append(0) print(max(scores))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
res = [] for nt in range(int(input())): n, t, a, b = map(int, input().split()) diff = list(map(int, input().split())) time = list(map(int, input().split())) count = diff.count(0) new = [(time[i], diff[i]) for i in range(n)] new.sort() ans = 0 curr = 0 k = 0 c = 0 while k < len(new) and curr <= t: d = new[k][0] - curr if d > 0: x = d // a if x <= count: if d % a == 0: ans = max(ans, c + x - 1) else: ans = max(ans, c + x) else: y = (d - count * a) // b if (d - count * a) % b == 0: ans = max(ans, c + y + count - 1) else: ans = max(ans, c + y + count) if new[k][1]: curr += b else: curr += a count -= 1 c += 1 k += 1 if curr <= t: ans = max(ans, c) print(min(ans, n))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
for _ in range(int(input())): n, t, a, b = map(int, input().split()) pt = [int(o) for o in input().split()] times = [int(o) for o in input().split()] d = dict() for i in range(n): try: d[times[i]].append(pt[i]) except: d[times[i]] = [pt[i]] times = sorted(list(set(times))) n1 = len(times) c0 = pt.count(0) c1 = pt.count(1) c11 = c1 c00 = c0 ans = [0] ct = 0 lol = 0 times.append(float("inf")) for i in range(n1): c0t = c0 c1t = c1 ctt = ct lolt = 0 trr = times[i] - ctt - 1 if trr > 0: x = trr // a if x <= c0t: c0t -= x trr = 0 ctt += x * a lolt += x else: ctt += c0t * a lolt += c0t trr -= c0t * a c0t = 0 x = trr // b if x <= c1t: c1t -= x trr = 0 ctt += x * b lolt += x else: ctt += c1t * b lolt += c1t trr -= c1t * b c1t = 0 if ctt <= t and ctt < times[i]: ans.append(lol + lolt) for j in d[times[i]]: if j == 1: ct += b c1 -= 1 lol += 1 else: ct += a c0 -= 1 lol += 1 if a * c00 + b * c11 <= t: ans.append(n) print(max(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys t = int(input()) ans = [0] * t for pi in range(t): n, T, a, b = map(int, sys.stdin.readline().split()) a1 = list(map(int, sys.stdin.readline().split())) a2 = list(map(int, sys.stdin.readline().split())) easy_count = a1.count(0) hard_count = n - easy_count tasks = sorted((t, x) for x, t in zip(a1, a2)) solved = 0 req_easy, req_hard = 0, 0 for i, (t, is_hard) in enumerate(tasks, start=1): rem = t - 1 - req_easy * a - req_hard * b if rem >= 0: c = min(easy_count - req_easy, rem // a) c += min(hard_count - req_hard, (rem - c * a) // b) solved = max(solved, c + req_easy + req_hard) if is_hard == 1: req_hard += 1 else: req_easy += 1 if easy_count * a + hard_count * b <= T: solved = n ans[pi] = solved print(*ans, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): N, T, a, b = map(int, input().split()) Es = list(map(int, input().split())) Ts = list(map(int, input().split())) Query.append((N, T, a, b, Es, Ts)) for N, T, a, b, Es, Ts in Query: P = [] remain_a = 0 remain_b = 0 for e, t in zip(Es, Ts): if e == 0: P.append((t, a)) remain_a += 1 else: P.append((t, b)) remain_b += 1 P.sort() t = 0 ans = 0 for i, (deadline, needtime) in enumerate(P): if t < deadline: remain_time = deadline - t - 1 a_c = min(remain_time // a, remain_a) remain_time -= a_c * a b_c = min(remain_time // b, remain_b) ans = max(ans, i + a_c + b_c) t += needtime if needtime == a: remain_a -= 1 else: remain_b -= 1 if t <= T: ans = N print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
m = int(input()) for _ in range(m): n, T, a, b = map(int, input().split()) diff = list(map(int, input().split())) t = list(map(int, input().split())) problem = [] for i in range(n): problem.append((diff[i], t[i])) problem.sort(key=lambda x: x[1]) possible = [0] * (n + 1) easy_count = [0] * (n + 1) hard_count = [0] * (n + 1) easy = 0 hard = 0 pre_easy = 0 pre_hard = 0 for i in range(n): time = problem[i][1] difficulty = problem[i][0] stop = time - 1 if i > 0 and problem[i - 1][1] != time: easy += pre_easy pre_easy = 0 hard += pre_hard pre_hard = 0 if easy * a + hard * b <= stop: possible[i] = 1 if difficulty == 0: pre_easy += 1 else: pre_hard += 1 easy_count[i] = easy hard_count[i] = hard stop = T easy += pre_easy hard += pre_hard easy_count[n] = easy hard_count[n] = hard if easy * a + hard * b <= stop: possible[n] = 1 ans = 0 for i in range(n): count = 0 if not possible[i]: continue time = problem[i][1] - 1 spend = easy_count[i] * a + hard_count[i] * b count += easy_count[i] + hard_count[i] d = time - spend if (easy_count[n] - easy_count[i]) * a > d: count += d // a else: d -= (easy_count[n] - easy_count[i]) * a count += easy_count[n] - easy_count[i] if (hard_count[n] - hard_count[i]) * b > d: count += d // b else: count += hard_count[n] - hard_count[i] ans = max(ans, count) if possible[n]: count = 0 i = n time = T spend = easy_count[i] * a + hard_count[i] * b count += easy_count[i] + hard_count[i] d = time - spend if (easy_count[n] - easy_count[i]) * a > d: count += d // a else: d -= (easy_count[n] - easy_count[i]) * a count += easy_count[n] - easy_count[i] if (hard_count[n] - hard_count[i]) * b > d: count += d // b else: count += hard_count[n] - hard_count[i] ans = max(ans, count) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
deadline = int(input()) for _ in range(deadline): n, T, a, b = map(int, input().split()) hard = list(map(int, input().split())) time = [(b if h else a) for h in hard] easies = n - sum(hard) deadlines = list(map(int, input().split())) tasks = [(deadlines[i], i) for i in range(n)] tasks.sort() filled_until = 0 max_pts = 0 done = 0 curr = 0 for deadline, i in tasks: if deadline > filled_until: if hard[i]: before = (deadline - filled_until - 1) // a max_pts = max(max_pts, done + min(easies, before)) else: max_pts = max(max_pts, done) filled_until += time[i] done += 1 if not hard[i]: easies -= 1 if filled_until <= T: max_pts = n print(max_pts)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n, T, a, b = mints() h = list(mints()) c = [0, 0] for i in h: c[i] += 1 i = 0 t = [None] * n for j in mints(): t[i] = j, i i += 1 t.sort() tt = 0 tmust = 0 cmust = 0 r = 0 for ii in range(len(t)): tn, i = t[ii] if tt < tn - 1: tt = tn - 1 left = tt - tmust if left >= 0: ac = min(left // a, c[0]) bc = min((left - ac * a) // b, c[1]) r = max(r, cmust + ac + bc) if h[i]: tmust += b c[1] -= 1 else: tmust += a c[0] -= 1 cmust += 1 if tt < T: tt = T left = tt - tmust if left >= 0: ac = min(left // a, c[0]) bc = min((left - ac * a) // b, c[1]) r = max(r, cmust + ac + bc) return r for i in range(mint()): print(solve())
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
def Input(): tem = input().split() ans = [] for it in tem: ans.append(int(it)) return ans TT = Input()[0] for tt in range(TT): n, T, a, b = Input() pro = Input() t = Input() ma = {} last = [0, 0] for i in range(n): if t[i] not in ma.keys(): ma[t[i]] = [0, 0] ma[t[i]][pro[i]] += 1 last[pro[i]] += 1 else: ma[t[i]][pro[i]] += 1 last[pro[i]] += 1 ma[T + 1] = [last[0], last[1]] ma_list = list(ma.keys()) ma_list.sort() m = len(ma_list) must = [0, 0] ans = 0 for i in range(m): remain = ma_list[i] - 1 must_cost = must[0] * a + must[1] * b if remain < must_cost: for j in range(2): must[j] += ma[ma_list[i]][j] last[j] -= ma[ma_list[i]][j] else: tem_must = sum(must) remain -= must_cost new_a = min(remain // a, last[0]) remain -= new_a * a new_b = min(remain // b, last[1]) ans = max(ans, tem_must + new_a + new_b) for j in range(2): must[j] += ma[ma_list[i]][j] last[j] -= ma[ma_list[i]][j] print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
for _ in range(int(input())): n, T, A, B = list(map(int, input().split(" "))) level = list(map(int, input().split(" "))) time = list(map(int, input().split(" "))) ques = [[i, j] for i, j in zip(level, time)] tota, totb = 0, 0 for i in ques: if i[0]: totb += 1 else: tota += 1 ques.sort(key=lambda x: x[1]) mandatory = {} prev = [0, 0] for i in range(n): prev[ques[i][0]] += 1 mandatory[ques[i][1]] = tuple(prev) mandatory[T] = tuple(prev) if 0 not in mandatory: mandatory[0] = tuple([0, 0]) timemandatory = sorted(list(mandatory)) for i in range(1, len(timemandatory)): if timemandatory[i] - 1 not in mandatory: mandatory[timemandatory[i] - 1] = mandatory[timemandatory[i - 1]] timemandatory = sorted(list(mandatory)) ans = 0 for i in timemandatory: if i >= 0: rema = tota - mandatory[i][0] remb = totb - mandatory[i][1] remtime = i - (mandatory[i][0] * A + mandatory[i][1] * B) curr = sum(mandatory[i]) if remtime >= 0: more = min(remtime // A, rema) remtime -= A * min(remtime // A, rema) more += min(remtime // B, remb) ans = max(ans, more + curr) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys t = int(sys.stdin.readline()) for _ in range(t): n, time, a, b = map(int, sys.stdin.readline().split()) nums = list(map(int, sys.stdin.readline().split())) l = list(map(int, sys.stdin.readline().split())) easy, hard, ans = 0, 0, 0 for i in range(n): if nums[i] == 0: l[i] = [l[i], 0] easy += 1 else: l[i] = [l[i], 1] hard += 1 l.sort() count_easy, count_hard = 0, 0 easy_man, hard_man = 0, 0 for i in range(n): count = 0 t = l[i][0] - 1 if a * easy_man + b * hard_man <= t: count += easy_man + hard_man t -= a * easy_man + b * hard_man easy_rem, hard_rem = easy - easy_man, hard - hard_man if easy_rem * a > t: count += t // a else: count += easy_rem t -= a * easy_rem if hard_rem * b > t: count += t // b else: count += hard_rem if l[i][1] == 1: hard_man += 1 else: easy_man += 1 ans = max(ans, count) if 1 == 1: count = 0 t = time if a * easy_man + b * hard_man <= t: count += easy_man + hard_man t -= a * easy_man + b * hard_man easy_rem, hard_rem = easy - easy_man, hard - hard_man if easy_rem * a > t: count += t // a else: count += easy_rem t -= a * easy_rem if hard_rem * b > t: count += t // b else: count += hard_rem if l[i][1] == 1: hard_man += 1 else: easy_man += 1 ans = max(ans, count) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
def case(): n, t, a, b = map(int, input().split()) qtype = list(map(int, input().split())) qtime = list(map(int, input().split())) z = [] easy, hard = 0, 0 for i in range(n): easy += qtype[i] == 0 hard += qtype[i] == 1 z.append([qtime[i], qtype[i]]) z.sort() ans = 0 esol, hsol = 0, 0 for i in z: if esol * a + hsol * b < i[0]: trem = i[0] - a * esol - b * hsol - 1 exe = min(easy - esol, trem // a) trem -= exe * a exh = min(hard - hsol, trem // b) ans = max(hsol + esol + exe + exh, ans) if i[1] == 0: esol += 1 else: hsol += 1 if esol * a + hsol * b <= t: trem = t - a * esol - b * hsol exe = min(easy - esol, trem // a) trem -= exe * a exh = min(hard - hsol, trem // b) ans = max(hsol + esol + exe + exh, ans) print(ans) for _ in range(int(input())): case()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys input = sys.stdin.readline for _ in range(int(input())): n, T, a, b = map(int, input().split()) ar = list(map(int, input().split())) tr = list(map(int, input().split())) br = [] easy = 0 hard = 0 for i in range(n): if ar[i] == 0: easy += 1 br.append([tr[i], a]) else: hard += 1 br.append([tr[i], b]) br.sort(key=lambda x: x[0]) su = [] cou = 0 for i in range(n): cou += br[i][1] su.append(cou) br.append([T + 1, 0]) ans = 0 ava_t = max(br[0][0] - 1, 0) xx = min(ava_t // a, easy) ava_t -= xx * a ans += xx xx = min(ava_t // b, hard) ans += xx for i in range(n): if br[i][1] == a: easy -= 1 else: hard -= 1 if br[i][0] == br[i + 1][0]: continue ava_t = br[i + 1][0] - 1 if su[i] > ava_t: continue tem = i + 1 ava_t -= su[i] tem_easy = easy tem_hard = hard xx = min(ava_t // a, tem_easy) ava_t -= xx * a tem += xx xx = min(ava_t // b, tem_hard) tem += xx ans = max(tem, ans) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
from sys import stderr m = int(input()) for _ in range(m): n, T, a, b = list(map(int, input().split())) c = [a, b] ty = list(map(int, input().split())) ca = n - sum(ty) cb = n - ca time = list(map(int, input().split())) items = list(zip(time, ty)) items.sort() ans = 0 t = 0 count = [0, 0] print(ca, cb, file=stderr) for i in range(n): if t > T: break if t < items[i][0]: temp = max(0, min(ca - count[0], (items[i][0] - 1 - t) // a)) ans = max( ans, count[0] + count[1] + temp + max(0, min(cb - count[1], (items[i][0] - 1 - t - temp * a) // b)), ) count[items[i][1]] += 1 t += c[items[i][1]] if t <= T: ans = n print(file=stderr) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
from sys import stdin, stdout m = int(stdin.readline().strip()) for _ in range(m): n, T, a, b = map(int, stdin.readline().split()) diff = list(map(int, stdin.readline().split())) mand = list(map(int, stdin.readline().split())) easy, hard = [], [] for i in range(n): if diff[i]: hard.append(mand[i]) else: easy.append(mand[i]) easy.sort() hard.sort() t, i, j, k = 0, 0, 0, 0 ans = 0 while t < T: _be, _bh = i < len(easy), j < len(hard) if _be and _bh: if t < hard[j]: if t < easy[i]: ans = max(ans, i + j) _t = t + b if ( _t <= T and (j + 1 == len(hard) or _t < hard[j + 1]) and _t < easy[i] ): k = i t += a i += 1 else: t = t + b - a * (i - k) j += 1 i = k elif _be: if t < easy[i]: ans = max(ans, i + j) i += 1 t += a elif _bh: if t < hard[j]: ans = max(ans, i + j) j += 1 t = t + b - a * (i - k) i = k else: break if t <= T and (i == len(easy) or t < easy[i]) and (j == len(hard) or t < hard[j]): ans = max(ans, i + j) stdout.write("{}\n".format(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
for _ in range(int(input())): n, t, a, b = map(int, input().split()) qi = list(map(int, input().split())) ti = list(map(int, input().split())) e = [] ea = qi.count(0) ha = qi.count(1) for i in range(n): e.append([ti[i], qi[i]]) e.sort() ans = 0 te = 0 s = [0, 0] for i in range(n): to = s[0] * a + s[1] * b if to >= e[i][0]: x = 0 else: p = min(ea, (e[i][0] - to - 1) // a) q = min(ha, (e[i][0] - to - p * a - 1) // b) x = sum(s) + p + q ans = max(ans, x) if e[i][1] == 0: s[0] += 1 ea -= 1 else: s[1] += 1 ha -= 1 to = s[0] * a + s[1] * b if to > t: x = 0 else: p = min(ea, (t - to) // a) q = min(ha, (t - to - p * a) // b) x = sum(s) + p + q ans = max(ans, x) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
case = int(input()) number = [] for i in range(case): c = input() c = c.split() n = int(c[0]) T = int(c[1]) a = int(c[2]) b = int(c[3]) problem = input() problem = list(map(int, problem.split())) time = input() time = list(map(int, time.split())) time_order = sorted(range(len(time)), key=lambda k: time[k]) time_step = [] easy_num = [] hard_num = [] easy_count = 0 hard_count = 0 time_step.append(time[time_order[0]]) if problem[time_order[0]] == 0: easy_count += 1 else: hard_count += 1 for j in range(1, n): if time[time_order[j]] != time[time_order[j - 1]]: easy_num.append(easy_count) hard_num.append(hard_count) easy_count = 0 hard_count = 0 time_step.append(time[time_order[j]]) if problem[time_order[j]] == 0: easy_count += 1 else: hard_count += 1 easy_num.append(easy_count) hard_num.append(hard_count) easy_sum = sum(easy_num) total_easy = easy_sum hard_sum = sum(hard_num) total_hard = hard_sum if easy_sum * a + hard_sum * b <= T: total = easy_sum + hard_sum else: total = 0 if total == 0: temp = 0 for j in range(len(time_step) - 1, -1, -1): T = time_step[j] - 1 easy_sum -= easy_num[j] hard_sum -= hard_num[j] solve_time = easy_sum * a + hard_sum * b easy_left = total_easy - easy_sum if solve_time <= T: temp = easy_sum + hard_sum rest = int((T - solve_time) / a) if rest <= easy_left: temp += rest else: temp += int((T - solve_time - easy_left * a) / b) + easy_left total = max(total, temp) number.append(total) for i in range(case): print(number[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
m = int(input()) for case_num in range(m): n, T, a, b = map(int, input().split(" ")) d = list(map(int, input().split(" "))) t = list(map(int, input().split(" "))) critical = set() for ti in t: critical.add(ti) if ti >= 1: critical.add(ti - 1) critical.add(T) critical = list(critical) critical.sort() k = len(critical) lut = dict() for i in range(k): lut[critical[i]] = i easy = [(0) for i in range(k)] hard = [(0) for i in range(k)] for i in range(n): idx = lut[t[i]] if d[i] == 0: easy[idx] += 1 else: hard[idx] += 1 mandatory_easy = 0 mandatory_hard = 0 total_hard = sum(d) total_easy = n - total_hard ans = 0 for i in range(k): mandatory_easy += easy[i] mandatory_hard += hard[i] time = critical[i] - mandatory_easy * a - mandatory_hard * b if time < 0: continue rest_easy = total_easy - mandatory_easy extra_easy = min(time // a, rest_easy) time -= extra_easy * a rest_hard = total_hard - mandatory_hard extra_hard = min(time // b, rest_hard) score = mandatory_easy + mandatory_hard + extra_easy + extra_hard ans = max(ans, score) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys def main(): def calcTotalTime(easyCnts, hardCnts): return a * easyCnts + b * hardCnts m = int(input()) allans = [] for _ in range(m): n, T, a, b = readIntArr() ease = readIntArr() t = readIntArr() et = [] easyCnts = 0 hardCnts = 0 for i in range(n): et.append([ease[i], t[i]]) if ease[i] == 0: easyCnts += 1 else: hardCnts += 1 et.append([-1, 0]) n += 1 et.sort(key=lambda x: x[1]) easyCumu = [(0) for _ in range(n)] hardCumu = [(0) for _ in range(n)] for i in range(n): if et[i][0] == 0: easyCumu[i] += 1 elif et[i][0] == 1: hardCumu[i] += 1 if i > 0: easyCumu[i] += easyCumu[i - 1] hardCumu[i] += hardCumu[i - 1] maxScore = 0 for i in range(n): e = easyCumu[i] h = hardCumu[i] if ( calcTotalTime(e, h) > T or i + 1 < n and calcTotalTime(e, h) >= et[i + 1][1] ): continue binn = easyCnts while binn > 0: while ( e + binn <= easyCnts and calcTotalTime(e + binn, h) <= T and (i == n - 1 or calcTotalTime(e + binn, h) < et[i + 1][1]) ): e += binn binn //= 2 binn = hardCnts while binn > 0: while ( h + binn <= hardCnts and calcTotalTime(e, h + binn) <= T and (i == n - 1 or calcTotalTime(e, h + binn) < et[i + 1][1]) ): h += binn binn //= 2 maxScore = max(maxScore, e + h) allans.append(maxScore) multiLineArrayPrint(allans) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] def makeArr(defaultVal, dimensionArr): dv = defaultVal da = dimensionArr if len(da) == 1: return [dv for _ in range(da[0])] else: return [makeArr(dv, da[1:]) for _ in range(da[0])] def queryInteractive(x, y): print("? {} {}".format(x, y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print("! {}".format(ans)) sys.stdout.flush() inf = float("inf") MOD = 10**9 + 7 for _abc in range(1): main()
IMPORT FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
m = int(input()) for ii in range(m): n, T, a, b = list(map(int, input().split())) score = [a, b] d = list(map(int, input().split())) t = list(map(int, input().split())) easy = 0 for d1 in d: if d1 == 0: easy += 1 diff = zip(t, d) diff = sorted(diff) cnt = 0 cur = 0 ans = 0 for i in range(n): t, d = diff[i] if cur < t and cur <= T: ans = max(cnt, ans) tmp = (t - 1 - cur) // a tmp = min(tmp, easy) ans = max(ans, cnt + tmp) cnt += 1 cur += score[d] if d == 0: easy -= 1 if cur <= T: ans = max(cnt, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
M = int(input()) def foo(A, B, T, free_easy, free_hard, req_easy, req_hard): ans = 0 T -= req_easy * A + req_hard * B if T < 0: return 0 ans += req_easy + req_hard can_easy = min(free_easy, T // A) ans += can_easy T -= A * can_easy can_hard = min(free_hard, T // B) ans += can_hard T -= A * can_hard return ans def solve(): [N, T, A, B] = list(map(int, input().split())) Hard = list(map(int, input().split())) Req = list(map(int, input().split())) problems = zip(Req, Hard) time_to_req = {} for t, h in problems: if t not in time_to_req: time_to_req[t] = [0, 0] time_to_req[t][h] += 1 free_hard = sum(Hard) free_easy = N - free_hard req_hard = 0 req_easy = 0 problems = [(t, time_to_req[t]) for t in time_to_req] problems = sorted(problems, key=lambda x: x[0]) ans = 0 for t, eh in problems: ans = max(ans, foo(A, B, t - 1, free_easy, free_hard, req_easy, req_hard)) [easy, hard] = eh free_hard -= hard req_hard += hard free_easy -= easy req_easy += easy ans = max(ans, foo(A, B, T, 0, 0, req_easy, req_hard)) return ans for _ in range(M): print(solve())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
for _ in range(int(input())): n, T, a, b = map(int, input().split()) diff = list(map(int, input().split())) time = list(map(int, input().split())) alla = diff.count(0) allb = n - alla prob = list(zip(time, diff)) prob.sort() cnt = 0 sm = 0 hard_x = 0 easy_x = 0 def calc(time): points = hard_x + easy_x time -= hard_x * b + easy_x * a a_avail = alla - easy_x b_avail = allb - hard_x dp = min(a_avail, time // a) points += dp time -= dp * a return points + min(b_avail, time // b) for pr in prob: if pr[0] > easy_x * a + hard_x * b: cnt = max(cnt, calc(pr[0] - 1)) if pr[1] == 1: hard_x += 1 else: easy_x += 1 if alla * a + allb * b <= T: cnt = max(cnt, calc(T)) print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys input = sys.stdin.readline t = int(input()) for i in range(t): n, T, a, b = map(int, input().split()) level = [int(i) for i in input().split()] time = [int(i) for i in input().split()] diff = zip(time, level) diff = sorted(diff) easy = level.count(0) hard = n - easy if easy * a + hard * b <= T: print(n) else: time = 0 question = 0 last = 0 hard1 = 0 for i in range(len(diff)): if diff[i][0] > time: question = max(last, question) new_time = (diff[i][0] - 1 - time) // a new_time = min(new_time, easy) question = max(question, new_time + last) if diff[i][1] == 0: time += a easy -= 1 last += 1 elif diff[i][1] == 1: time += b last += 1 print(question)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys input = sys.stdin.readline def solve(time, measy, mhard, teasy, thard): if measy * a + mhard * b > time: return 0 ret = measy + mhard time -= measy * a + mhard * b x = min(time // a, teasy - measy) ret += x time -= a * x y = min(time // b, thard - mhard) ret += y return ret t = int(input()) for _ in range(t): n, T, a, b = map(int, input().split()) d = list(map(int, input().split())) m = list(map(int, input().split())) A = [] for i in range(n): A.append(m[i] * 10 + d[i]) A.sort() teasy = d.count(0) thard = d.count(1) measy, mhard = 0, 0 ans = 0 for i in range(n): time, dd = divmod(A[i], 10) time -= 1 cur = solve(time, measy, mhard, teasy, thard) ans = max(ans, cur) if dd: mhard += 1 else: measy += 1 cur = solve(T, measy, mhard, teasy, thard) ans = max(ans, cur) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
M = int(input()) for _ in range(M): n, T, a, b = map(int, input().split()) ns = [(a if x == "0" else b) for x in input().split()] ts = [int(x) for x in input().split()] sort = sorted(list(zip(ns, ts)), key=lambda x: x[1]) ns = [x[0] for x in sort] ts = [x[1] for x in sort] + [T + 1] best = 0 tot = 0 n_a = n_b = 0 for x in ns: if x == a: n_a += 1 else: n_b += 1 def fill(x): if x <= 0: return 0 if n_a * a >= x: return x // a else: x -= n_a * a if n_b * b >= x: return n_a + x // b return n_a + n_b for i in range(n): if tot < ts[i]: best = max(best, i + fill(ts[i] - 1 - tot)) if ts[i] == ts[i + 1]: tot += ns[i] if ns[i] == a: n_a -= 1 else: n_b -= 1 continue tot += ns[i] if ns[i] == a: n_a -= 1 else: n_b -= 1 if tot <= ts[i]: best = max(best, i + 1 + fill(ts[i] - tot)) if tot < ts[i + 1]: best = max(best, i + 1 + fill(ts[i + 1] - 1 - tot)) assert n_a == n_b == 0 if sum(ns) <= T: best = n if a > T: best = 0 print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
def solve(): nproblems, tottime, a, b = map(int, input().split()) diffls = list(map(int, input().split())) mandls = list(map(int, input().split())) ls = [] for i in range(nproblems): ls.append((mandls[i], diffls[i])) ls.sort() nez = diffls.count(0) mandtime = 0 ptr = 0 ezmand = 0 best = 0 while ptr < nproblems: mandtime += b if ls[ptr][1] else a ezmand += 0 if ls[ptr][1] else 1 while ptr + 1 < nproblems and ls[ptr + 1][0] == ls[ptr][0]: ptr += 1 mandtime += b if ls[ptr][1] else a ezmand += 0 if ls[ptr][1] else 1 if ptr + 1 < nproblems: timeleft = ls[ptr + 1][0] - mandtime - 1 else: timeleft = tottime - mandtime if timeleft >= 0 and mandtime <= tottime: bonusa = min(timeleft // a, nez - ezmand) tafta = mandtime + bonusa * a if ptr + 1 < nproblems: timeleftb = ls[ptr + 1][0] - tafta - 1 else: timeleftb = tottime - tafta bonusb = max(0, min(timeleftb // b, nproblems - (ptr + 1 + bonusa))) best = max(best, ptr + 1 + bonusa + bonusb) ptr += 1 timeleft = ls[0][0] - 1 bonusa = min(timeleft // a, nez) tafta = bonusa * a timeleftb = ls[0][0] - tafta - 1 bonusb = max(0, min(timeleftb // b, nproblems - nez)) best = max(best, bonusa + bonusb) print(best) for tcase in range(int(input())): solve()
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
for t in range(int(input())): n, ti, a, b = map(int, input().split()) aa = list(map(int, input().split())) bb = list(map(int, input().split())) c = [] count_eb = 0 count_hb = 0 count_ef = 0 count_hf = 0 for i in range(n): c.append([bb[i], aa[i]]) if aa[i] == 1: count_hb += 1 else: count_eb += 1 c.sort() c.append([ti + 1, 0]) count = 0 store = 0 for i in range(n + 1): diff = count_hf * b + count_ef * a count = 0 if diff <= c[i][0] - 1 and diff <= ti: count = i vac_e = abs(diff - c[i][0] + 1) if count_eb * a > vac_e: rem = vac_e // a else: rem = count_eb count += rem vac_h = abs(vac_e - rem * a) if count_hb * b <= vac_h: count += count_hb else: count += vac_h // b store = max(store, count) if c[i][1] == 1: count_hb -= 1 count_hf += 1 else: count_eb -= 1 count_ef += 1 print(store)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
t = int(input()) for _ in range(t): n, T, a, b = map(int, input().split()) h1 = [int(x) for x in input().split()] h2 = [int(x) for x in input().split()] h1.append(0) h2.append(T + 1) h = sorted(zip(h2, h1)) c0 = h1.count(0) - 1 c1 = h1.count(1) ans = 0 ptr = 0 man = 0 c = 0 for i in range(0, len(h)): brk = h[i][0] - 1 ex = max(0, brk - man) ex_task = 0 t1 = min(c0, ex // a) ex -= t1 * a t2 = min(c1, ex // b) ex -= t2 * b ex_task += t1 + t2 if brk >= man: ans = max(ans, ex_task + i) if h[i][1] == 1: man += b c1 -= 1 else: man += a c0 -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
for _ in range(int(input())): n, t, a, b = map(int, input().split()) r = list(map(int, input().split())) s = list(map(int, input().split())) d = dict() hd = 0 es = 0 for i in range(n): if s[i] in d.keys(): d[s[i]][r[i]] += 1 else: d[s[i]] = [0, 0] d[s[i]][r[i]] += 1 if r[i] == 0: es += 1 else: hd += 1 if 0 not in d.keys(): d[0] = [0, 0] dd = list(d.keys()) dd.sort() for i in range(1, len(dd)): d[dd[i]][0] += d[dd[i - 1]][0] d[dd[i]][1] += d[dd[i - 1]][1] ans = 0 for i in range(1, len(dd)): easy = d[dd[i - 1]][0] hard = d[dd[i - 1]][1] time = dd[i] - 1 if a * easy + b * hard <= time: a_plus = (time - a * easy - b * hard) // a a_plus = min(a_plus, es - easy) b_plus = (time - a * easy - b * hard - a_plus * a) // b b_plus = min(b_plus, hd - hard) ans = max(ans, easy + hard + a_plus + b_plus) if a * es + b * hd <= t: ans = es + hd print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF NUMBER FUNC_CALL VAR ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
class questions: def __init__(self, level, req_time): self.level = level self.Ti = req_time for _ in range(int(input())): n, N, a, b = map(int, input().split()) level = list(map(int, input().split())) Ti = list(map(int, input().split())) easy_q = level.count(0) hard_q = level.count(1) ql = [] for i in range(n): ql.append(questions(level[i], Ti[i])) ql.sort(key=lambda x: x.Ti) req_time = 0 solved_easy = 0 solved_hard = 0 best_point = 0 last_t_leave = 0 last_idx = 0 for i in range(n + 1): if i == n: t_leave = N else: t_leave = ql[i].Ti - 1 if t_leave == last_t_leave: continue last_t_leave = t_leave points = 0 for j in range(last_idx, i): if ql[j].level == 0: solved_easy += 1 req_time += a else: solved_hard += 1 req_time += b last_idx = i if req_time <= t_leave: points += solved_easy + solved_hard available_time = t_leave - req_time pos_easy = int(available_time / a) other_easy = min(pos_easy, easy_q - solved_easy) points += other_easy available_time -= other_easy * a pos_hard = int(available_time / b) other_hard = min(pos_hard, hard_q - solved_hard) points += other_hard else: points = 0 best_point = max(best_point, points) print(best_point)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
m = int(input()) for i in range(m): n, T, a, b = list(map(int, input().split())) is_hard = list(map(int, input().split())) total_hard = sum(is_hard) total_easy = n - total_hard time_mandatory = list(map(int, input().split())) mandatory_times = sorted( [(time_mandatory[i], i) for i in range(len(time_mandatory))] ) mandatory_times.append((T, -1)) maximal_points = 0 min_easy = 0 min_hard = 0 for i, (time, problem_no) in enumerate(mandatory_times): bad = False if i != len(mandatory_times) - 1 and mandatory_times[i + 1][0] == time: bad = True remaining_easy = total_easy - min_easy remaining_hard = total_hard - min_hard remaining_time = time - 1 - min_easy * a - min_hard * b if remaining_time >= 0: if remaining_time >= a * remaining_easy: maximal_points = max( maximal_points, min_easy + min_hard + remaining_easy + min((remaining_time - a * remaining_easy) // b, remaining_hard), ) else: maximal_points = max( maximal_points, min_easy + min_hard + remaining_time // a ) if problem_no == -1: min_easy = min_easy elif is_hard[problem_no] == 1: min_hard += 1 else: min_easy += 1 if bad: continue remaining_easy = total_easy - min_easy remaining_hard = total_hard - min_hard remaining_time = time - min_easy * a - min_hard * b if remaining_time >= 0: if remaining_time >= a * remaining_easy: maximal_points = max( maximal_points, min_easy + min_hard + remaining_easy + min((remaining_time - a * remaining_easy) // b, remaining_hard), ) else: maximal_points = max( maximal_points, min_easy + min_hard + remaining_time // a ) print(maximal_points)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys def input(): return sys.stdin.readline().rstrip() def slv(): n, T, a, b = map(int, input().split()) problem_type = list(map(int, input().split())) reqtime = list(map(int, input().split())) problems = [] all_a, all_b = 0, 0 for i in range(n): if problem_type[i] == 0: all_a += 1 problems.append((a, reqtime[i])) else: all_b += 1 problems.append((b, reqtime[i])) reqtime.append(T + 1) reqtime = list(set(reqtime)) reqtime.sort() problems.sort(key=lambda x: x[1]) def max_getter(time_allowed, must_a, must_b, all_a, all_b): assert must_a <= all_a assert must_b <= all_b if a * must_a + b * must_b > time_allowed: return -1 else: time_left = time_allowed - (a * must_a + b * must_b) free_a, free_b = all_a - must_a, all_b - must_b if time_left < free_a * a: return must_a + must_b + time_left // a else: time_left -= free_a * a return must_a + must_b + free_a + min(time_left // b, free_b) ans = 0 idx = 0 must_a, must_b = 0, 0 for v in reqtime: while idx < n and problems[idx][1] < v: if problems[idx][0] == a: must_a += 1 else: must_b += 1 idx += 1 ans = max(ans, max_getter(v - 1, must_a, must_b, all_a, all_b)) print(ans) return def main(): t = int(input()) for i in range(t): slv() return main()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
t = int(input()) for _ in range(t): n, T, a, b = list(map(int, input().split(" "))) task_t = list(map(int, input().split(" "))) ness = list(map(int, input().split(" "))) perm = sorted(list(range(n)), key=lambda i: ness[i]) score = 0 tot_hard = sum(task_t) tot_easy = n - tot_hard must_easy = 0 must_hard = 0 for i in range(n): if i > 0 and ness[perm[i]] == ness[perm[i - 1]]: if task_t[perm[i]] == 0: must_easy += 1 else: must_hard += 1 continue tm = ness[perm[i]] - 1 req_time = must_easy * a + must_hard * b if req_time > tm: if task_t[perm[i]] == 0: must_easy += 1 else: must_hard += 1 continue extra_time = tm - req_time extra_easy = min(extra_time // a, tot_easy - must_easy) extra_time -= a * extra_easy extra_hard = min(extra_time // b, tot_hard - must_hard) score = max(score, extra_easy + extra_hard + must_easy + must_hard) if task_t[perm[i]] == 0: must_easy += 1 else: must_hard += 1 if tot_easy * a + tot_hard * b <= T: score = n print(score)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $n$ problems that can be solved in $T$ minutes. Thus, the exam begins at time $0$ and ends at time $T$. Petya can leave the exam at any integer time from $0$ to $T$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $a$ minutes to solve any easy problem; hard problems — Petya takes exactly $b$ minutes ($b > a$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $x$, then it will be solved at time $x+a$. Similarly, if at a time $x$ Petya starts to solve a hard problem, then it will be solved at time $x+b$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $t_i$ ($0 \le t_i \le T$) at which it will become mandatory (required). If Petya leaves the exam at time $s$ and there is such a problem $i$ that $t_i \le s$ and he didn't solve it, then he will receive $0$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $t_i \le s$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $s$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $n=2$, $T=5$, $a=2$, $b=3$, the first problem is hard and $t_1=3$ and the second problem is easy and $t_2=2$. Then: if he leaves at time $s=0$, then he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=1$, he will receive $0$ points since he will not have time to solve any problems; if he leaves at time $s=2$, then he can get a $1$ point by solving the problem with the number $2$ (it must be solved in the range from $0$ to $2$); if he leaves at time $s=3$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=4$, then he will receive $0$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $s=5$, then he can get $2$ points by solving all problems. Thus, the answer to this test is $2$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. -----Input----- The first line contains the integer $m$ ($1 \le m \le 10^4$) — the number of test cases in the test. The next lines contain a description of $m$ test cases. The first line of each test case contains four integers $n, T, a, b$ ($2 \le n \le 2\cdot10^5$, $1 \le T \le 10^9$, $1 \le a < b \le 10^9$) — the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $n$ numbers $0$ or $1$, separated by single space: the $i$-th number means the type of the $i$-th problem. A value of $0$ means that the problem is easy, and a value of $1$ that the problem is hard. The third line of each test case contains $n$ integers $t_i$ ($0 \le t_i \le T$), where the $i$-th number means the time at which the $i$-th problem will become mandatory. It is guaranteed that the sum of $n$ for all test cases does not exceed $2\cdot10^5$. -----Output----- Print the answers to $m$ test cases. For each set, print a single integer — maximal number of points that he can receive, before leaving the exam. -----Example----- Input 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 Output 3 2 1 0 1 4 0 1 2 1
import sys input = sys.stdin.readline t = int(input()) for testcases in range(t): n, T, a, b = list(map(int, input().split())) A = list(map(int, input().split())) L = list(map(int, input().split())) LCAN = [T] EASY = [] HARD = [] for i in range(n): if A[i] == 0: EASY.append(L[i]) else: HARD.append(L[i]) if L[i] > 1: LCAN.append(L[i] - 1) LCAN = sorted(set(LCAN)) EASY.sort() HARD.sort() eind = 0 hind = 0 LENE = len(EASY) LENH = len(HARD) needtime = 0 ANS = 0 for time in LCAN: while eind < LENE and EASY[eind] <= time: needtime += a eind += 1 while hind < LENH and HARD[hind] <= time: needtime += b hind += 1 if time < needtime: continue else: rest = time - needtime score = eind + hind if (LENE - eind) * a >= rest: score += rest // a else: score = LENE + hind rest -= (LENE - eind) * a score += min(LENH - hind, rest // b) ANS = max(ANS, score) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
In the game of Mastermind, there are two players — Alice and Bob. Alice has a secret code, which Bob tries to guess. Here, a code is defined as a sequence of n colors. There are exactly n+1 colors in the entire universe, numbered from 1 to n+1 inclusive. When Bob guesses a code, Alice tells him some information about how good of a guess it is, in the form of two integers x and y. The first integer x is the number of indices where Bob's guess correctly matches Alice's code. The second integer y is the size of the intersection of the two codes as multisets. That is, if Bob were to change the order of the colors in his guess, y is the maximum number of indices he could get correct. For example, suppose n=5, Alice's code is [3,1,6,1,2], and Bob's guess is [3,1,1,2,5]. At indices 1 and 2 colors are equal, while in the other indices they are not equal. So x=2. And the two codes have the four colors 1,1,2,3 in common, so y=4. <image> Solid lines denote a matched color for the same index. Dashed lines denote a matched color at a different index. x is the number of solid lines, and y is the total number of lines. You are given Bob's guess and two values x and y. Can you find one possibility of Alice's code so that the values of x and y are correct? Input The first line contains a single integer t (1≤ t≤ 1000) — the number of test cases. Next 2t lines contain descriptions of test cases. The first line of each test case contains three integers n,x,y (1≤ n≤ 10^5, 0≤ x≤ y≤ n) — the length of the codes, and two values Alice responds with. The second line of each test case contains n integers b_1,…,b_n (1≤ b_i≤ n+1) — Bob's guess, where b_i is the i-th color of the guess. It is guaranteed that the sum of n across all test cases does not exceed 10^5. Output For each test case, on the first line, output "YES" if there is a solution, or "NO" if there is no possible secret code consistent with the described situation. You can print each character in any case (upper or lower). If the answer is "YES", on the next line output n integers a_1,…,a_n (1≤ a_i≤ n+1) — Alice's secret code, where a_i is the i-th color of the code. If there are multiple solutions, output any. Example Input 7 5 2 4 3 1 1 2 5 5 3 4 1 1 2 1 2 4 0 4 5 5 3 3 4 1 4 2 3 2 3 6 1 2 3 2 1 1 1 1 6 2 4 3 3 2 1 1 1 6 2 6 1 1 3 2 1 1 Output YES 3 1 6 1 2 YES 3 1 1 1 2 YES 3 3 5 5 NO YES 4 4 4 4 3 1 YES 3 1 3 1 7 7 YES 2 3 1 1 1 1 Note The first test case is described in the statement. In the second test case, x=3 because the colors are equal at indices 2,4,5. And y=4 because they share the colors 1,1,1,2. In the third test case, x=0 because there is no index where the colors are the same. But y=4 because they share the colors 3,3,5,5. In the fourth test case, it can be proved that no solution exists.
t = int(input()) for q in range(t): n, x, y = map(int, input().split()) Bob = list(map(int, input().split())) Cnt = [[0, i] for i in range(n + 1)] Ans = [-1] * n Occ = [[] for i in range(n + 1)] for i in range(n): Bob[i] -= 1 Cnt[Bob[i]][0] += 1 Occ[Bob[i]].append(i) Cnt.sort(reverse=True) lvl = Cnt[0][0] i = 0 xcpy = x while x > 0: while x > 0 and Cnt[i][0] >= lvl: Cnt[i][0] -= 1 col = Cnt[i][1] Ans[Occ[col].pop()] = col x -= 1 i += 1 if i == n or Cnt[i][0] < lvl: lvl = Cnt[0][0] i = 0 Cnt.sort(reverse=True) x = xcpy if Cnt[0][0] * 2 - (n - x) > n - y: print("NO") continue Pos = [] Clr = [] for i in range(n): if Ans[i] == -1: Pos.append([Bob[i], i]) Clr.append(Bob[i]) m = len(Pos) Pos.sort() Clr.sort() offset = m // 2 nocnt = n - y nocolor = Cnt[-1][1] for i in range(m): pos = Pos[i][1] c = Clr[(offset + i) % m] if i + nocnt == m: Ans[pos] = nocolor nocnt -= 1 continue if Pos[i][0] == c: assert nocnt > 0 Ans[pos] = nocolor nocnt -= 1 else: Ans[pos] = c assert nocnt == 0 print("YES") for c in Ans: print(c + 1, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
t = int(input()) while t: n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) d1 = [0] * n d2 = [0] * n j = n - 1 res = [] for i in range(n - 1, -1, -1): while j >= 0 and b[j] >= a[i]: res.append(b[j]) j -= 1 d1[i] = res[-1] - a[i] d2[i] = res[0] - a[i] c = n - i h = n - (j + 1) if c == h: res = [] print(" ".join(map(str, d1))) print(" ".join(map(str, d2))) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
def solve(): n = int(input()) a = list(map(int, input().split())) + [10**10] b = list(map(int, input().split())) + [10**10] min_ = n * [0] max_ = n * [0] r = -1 for i in range(n): while r < n and a[i] > b[r + 1]: r += 1 min_[i] = r + 1 ind = 0 cur = n for i in range(1, n + 1): max_[n - i] = max(cur - 1, 0) if min_[n - i] == n - i: cur = n - i for i in range(n): print(b[min_[i]] - a[i], end=" ") print() for i in range(n): print(b[max_[i]] - a[i], end=" ") print() t = int(input()) for i in range(t): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
testcases = int(input()) for t in range(testcases): n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] mini = [] i = 0 for x in a: while x > b[i]: i += 1 mini.append(b[i] - x) maxi = [] j = 0 for k in range(n - 1): j = max(j, k) while j + 1 < n and a[j + 1] <= b[j]: j += 1 maxi.append(b[j] - a[k]) maxi.append(b[n - 1] - a[n - 1]) for x in mini: print(x, end=" ") print() for x in maxi: print(x, end=" ") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
T = int(input()) for _ in range(T): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) start = end = n - 1 m = [] M = [] for i in range(n - 1, -1, -1): while b[start] >= a[i] and start >= 0: start -= 1 start += 1 m.append(b[start] - a[i]) M.append(b[end] - a[i]) if start == i: start -= 1 end = start ans1 = "" ans2 = "" for i in range(n - 1, 0, -1): ans1 += str(m[i]) + " " ans2 += str(M[i]) + " " ans1 += str(m[0]) ans2 += str(M[0]) print(ans1) print(ans2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR STRING VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) mini = [0] * n maxi = [0] * n up = 0 down = 0 while up < n and down < n: if b[down] < a[up]: down += 1 else: maxi[up] = b[down] - a[up] if down + 1 < n and a[down + 1] <= b[down]: down += 1 else: up += 1 up = n - 1 down = n - 1 while up >= 0 and down >= 0: mini[up] = b[down] - a[up] if down - 1 >= 0 and b[down - 1] >= a[up]: down -= 1 else: up -= 1 print(*mini) print(*maxi)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) dp1, dp2 = [0] * n, [0] * n j = 0 for i in range(n): while b[j] < a[i]: j += 1 dp1[i] = b[j] - a[i] j = n - 1 for i in reversed(range(n)): dp2[i] = b[j] - a[i] if b[i - 1] < a[i]: j = i - 1 print(*dp1) print(*dp2)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
def main(): for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) d1, d2 = [], [] j = 0 for i in range(n): while b[j] < a[i]: j += 1 print(b[j] - a[i], end=" ") print() j = 0 for i in range(n): j = max(j, i) while j + 1 < n and a[j + 1] <= b[j]: j += 1 print(b[j] - a[i], end=" ") print() main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) j = 0 d = [] g = [0] * n for i in range(n): while a[i] > b[j]: j += 1 d.append(b[j] - a[i]) g[n - 1] = b[n - 1] - a[n - 1] j = n - 1 for i in range(n - 2, -1, -1): if a[i + 1] <= b[i]: g[i] = b[j] - a[i] else: g[i] = b[i] - a[i] j = i print(" ".join(list(map(str, d)))) print(" ".join(list(map(str, g))))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
import sys input = sys.stdin.readline def solve(): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) mi, ma = [], [] f = 0 for i in range(n - 1): if A[i + 1] > B[i]: val = B[i] for j in range(f, i + 1): ma.append(val - A[j]) f = i + 1 for j in range(f, n): ma.append(B[-1] - A[j]) j = 0 for i in range(n): while j < n and B[j] < A[i]: j += 1 mi.append(B[j] - A[i]) print(*mi) print(*ma) return for _ in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
def ss_min(l, a): n = len(l) def ceiling_of_element(x): start = 0 end = n - 1 res = -1 while start <= end: mid = (start + end) // 2 if a[mid] == x: return a[mid] elif x > a[mid]: start = mid + 1 else: res = mid end = mid - 1 return a[res] ans = [] for x in l: ans.append(ceiling_of_element(x) - x) return ans def ss_max(a, b): n = len(a) t = [(True) for _ in range(n)] for i in range(n - 1, 0, -1): if a[i] <= b[i - 1]: t[i] = True else: t[i] = False t_t = [i for i in range(n)] last_f = n for i in range(n - 1, -1, -1): t_t[i] = last_f - 1 if t[i] == False: last_f = i ans = [] for i in range(n): ans.append(b[t_t[i]] - a[i]) return ans for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) ans_min = ss_min(a, b) ans_max = ss_max(a, b) print(*ans_min) print(*ans_max)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
def solve(): num = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) ans1 = [] ans2 = [0] * num pointer = 0 for i in l1: while l2[pointer] < i: pointer += 1 ans1.append(l2[pointer] - i) maxx = [] for i in range(num - 1, -1, -1): x = l1[i] while len(l2) and l2[-1] >= x: maxx.append(l2.pop()) ans2[i] = maxx[0] - x maxx.pop() print(*ans1) print(*ans2) n = int(input()) for i in range(n): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
import sys def findleastgreater(lst, end, num): l, r = 1, end if lst[0] >= num: return 0 while l < r: mid = l + r >> 1 if lst[mid] >= num and lst[mid - 1] < num: return mid elif lst[mid] >= num: r = mid - 1 else: l = mid + 1 return l epoch = int(sys.stdin.readline()) for _ in range(epoch): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().strip().split())) d = list(map(int, sys.stdin.readline().strip().split())) mi, ma = [], [] end = n - 1 block = n for i in range(n - 1, -1, -1): num = a[i] idx = findleastgreater(d, end, num) end = idx ma.append(d[block - 1] - num) if i <= idx: block = idx mi.append(d[idx] - num) for i in mi[::-1]: print(i, end=" ") print("") for i in ma[::-1]: print(i, end=" ") print("")
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR STRING
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
t = int(input()) while t > 0: n = int(input()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] dmin = [] dmax = [] check = [] j = 0 for i in range(n): while a[i] > b[j]: j += 1 check.append(n - j) dmin.append(b[j] - a[i]) dmax.append(0) x = 1 m = b[-1] for i in range(n - 1, -1, -1): dmax[i] = m - a[i] if check[i] - x == 0: m = b[n - check[i] - 1] x += 1 for i in dmin: print(i, end=" ") print() for i in dmax: print(i, end=" ") print() t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
t = int(input()) while t > 0: n = int(input()) a = list(map(int, input().split(" "))) b = list(map(int, input().split(" "))) bptr = n - 1 aptr = n - 1 store = [] dmin = [0] * n dmax = [0] * n a_used = 0 while aptr >= 0: while bptr >= 0 and b[bptr] >= a[aptr]: store.append(b[bptr]) bptr -= 1 dmin[aptr] = store[-1] - a[aptr] dmax[aptr] = store[0] - a[aptr] a_used += 1 if a_used == len(store): store = [] a_used = 0 aptr -= 1 for i in range(n): if i < n - 1: print(dmin[i], end=" ") else: print(dmin[i]) for i in range(n): if i < n - 1: print(dmax[i], end=" ") else: print(dmax[i]) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
standard_input = """4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 """ for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) greaterThan = [(0) for _ in range(n)] k = n - 1 for i in range(n - 1, -1, -1): while k > 0 and b[k - 1] >= a[i]: k -= 1 greaterThan[i] = k print(*(b[greaterThan[i]] - a[i] for i in range(n))) bestSpot = [(0) for _ in range(n)] highestSpot = n - 1 maximum = [(0) for _ in range(n)] for i in range(n - 1, -1, -1): while bestSpot[highestSpot]: highestSpot -= 1 ans = b[highestSpot] - a[i] maximum[i] = ans k = greaterThan[i] while bestSpot[k]: k = bestSpot[k] bestSpot[k] = k + 1 bestSpot[greaterThan[i]] = k + 1 print(*maximum)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2, \dots, a_n$, which is sorted in non-descending order. You decided to perform the following steps to create array $b_1, b_2, \dots, b_n$: Create an array $d$ consisting of $n$ arbitrary non-negative integers. Set $b_i = a_i + d_i$ for each $b_i$. Sort the array $b$ in non-descending order. You are given the resulting array $b$. For each index $i$, calculate what is the minimum and maximum possible value of $d_i$ you can choose in order to get the given array $b$. Note that the minimum (maximum) $d_i$-s are independent of each other, i. e. they can be obtained from different possible arrays $d$. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of arrays $a$, $b$ and $d$. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$; $a_i \le a_{i+1}$) — the array $a$ in non-descending order. The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10^9$; $b_i \le b_{i+1}$) — the array $b$ in non-descending order. Additional constraints on the input: there is at least one way to obtain the array $b$ from the $a$ by choosing an array $d$ consisting of non-negative integers; the sum of $n$ doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print two lines. In the first line, print $n$ integers $d_1^{min}, d_2^{min}, \dots, d_n^{min}$, where $d_i^{min}$ is the minimum possible value you can add to $a_i$. Secondly, print $n$ integers $d_1^{max}, d_2^{max}, \dots, d_n^{max}$, where $d_i^{max}$ is the maximum possible value you can add to $a_i$. All $d_i^{min}$ and $d_i^{max}$ values are independent of each other. In other words, for each $i$, $d_i^{min}$ is just the minimum value among all possible values of $d_i$. -----Examples----- Input 4 3 2 3 5 7 11 13 1 1000 5000 4 1 2 3 4 1 2 3 4 4 10 20 30 40 22 33 33 55 Output 5 4 2 11 10 8 4000 4000 0 0 0 0 0 0 0 0 12 2 3 15 23 13 3 15 -----Note----- In the first test case, in order to get $d_1^{min} = 5$, we can choose, for example, $d = [5, 10, 6]$. Then $b$ $=$ $[2+5,3+10,5+6]$ $=$ $[7,13,11]$ $=$ $[7,11,13]$. For $d_2^{min} = 4$, we can choose $d$ $=$ $[9, 4, 8]$. Then $b$ $=$ $[2+9,3+4,5+8]$ $=$ $[11,7,13]$ $=$ $[7,11,13]$.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) i = 0 first = 0 last = n - 1 dmin = [] dmax = [] while i < n: if a[i] <= b[first]: dmin.append(b[first] - a[i]) i += 1 else: first += 1 j = n - 1 while j >= 0: if j == n - 1: dmax.append(b[last] - a[j]) j -= 1 elif b[j] < a[j + 1]: last = j dmax.append(b[last] - a[j]) j -= 1 else: dmax.append(b[last] - a[j]) j -= 1 print(*dmin) print(*dmax[::-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER