message stringlengths 2 39.6k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 450 109k | cluster float64 2 2 | __index_level_0__ int64 900 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1 | instruction | 0 | 96,253 | 2 | 192,506 |
Tags: brute force, data structures, sortings
Correct Solution:
```
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
dp = []
add = []
am = []
def build(pos, l,r):
global dp
global am
if l == r - 1:
dp[pos] = -am[l][1]
return
mid = int((l+r)/2)
build (pos*2+1,l,mid)
build (pos*2+2,mid,r)
dp[pos] = max(dp[pos*2+1],dp[pos*2+2])
def push(pos, l,r):
global dp
global add
if add[pos] != 0:
if r- l > 1:
add[pos*2+1] += add[pos]
dp[pos*2+1] += add[pos]
add[pos*2+2] += add[pos]
dp[pos*2+2] += add[pos]
add[pos] = 0
def update(pos, l,r,L,R, val):
global dp
global add
if L>=R:
return
if l == L and r == R:
add[pos] += val
dp[pos] += val
push(pos, l,r)
return
push(pos, l,r)
mid = int((l+r)/2)
update(pos*2+1, l, mid, L, min(R,mid), val)
update(pos*2+2, mid, r, max(L,mid), R, val)
dp[pos] = max(dp[pos*2+1],dp[pos*2+2])
def main():
global dp
global am
global add
n,m,k = list(map(int, stdin.readline().split()))
AMT = 1000002
dp = [0] * (m *4 + 2)
add = [0] * (m *4 + 2)
WP = [-1] * AMT
AM = [-1] * AMT
am2 = []
PWN = [[] for _ in range(AMT)]
max_atk = -1
for _ in range(n):
power, cost = list(map(int, stdin.readline().split()))
if WP[power] == -1 or (WP[power] != -1 and WP[power] > cost):
WP[power] = cost
if power > max_atk:
max_atk = power
for _ in range(m):
power, cost = list(map(int, stdin.readline().split()))
if AM[power] == -1 or (AM[power] != -1 and AM[power] > cost):
AM[power] = cost
for _ in range(k):
atk, df, gold = list(map(int, stdin.readline().split()))
PWN[atk+1].append((df, gold))
for i in range(AMT):
if AM[i] != -1:
am.append((i, AM[i]))
am2.append(i)
m = len(am)
build(0,0,m)
mx = -999999999999
for i in range(AMT):
if i > max_atk:
break
for a,b in PWN[i]:
right = bisect_right(am2, a)
update(0, 0, m, right, m, b)
if WP[i] == -1:
continue
temp = dp[0]
if temp - WP[i] > mx:
mx = temp - WP[i]
stdout.write(str(mx))
main()
``` | output | 1 | 96,253 | 2 | 192,507 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1 | instruction | 0 | 96,254 | 2 | 192,508 |
Tags: brute force, data structures, sortings
Correct Solution:
```
from bisect import bisect_right
from operator import itemgetter
# quick input by @pajenegod
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
class SegmTree:
def __init__(self, size):
N = 1
h = 0
while N < size:
N <<= 1
h += 1
self.N = N
self.h = h
self.t = [0] * (2 * N)
self.d = [0] * N
def apply(self, p, value):
self.t[p] += value
if (p < self.N):
self.d[p] += value
def build(self, p):
t = self.t
d = self.d
while p > 1:
p >>= 1
t[p] = max(t[p<<1], t[p<<1|1]) + d[p]
def rebuild(self):
t = self.t
for p in reversed(range(1, self.N)):
t[p] = max(t[p<<1], t[p<<1|1])
def push(self, p):
d = self.d
for s in range(self.h, 0, -1):
i = p >> s
if d[i] != 0:
self.apply(i<<1, d[i])
self.apply(i<<1|1, d[i])
d[i] = 0
def inc(self, l, r, value):
if l >= r:
return
l += self.N
r += self.N
l0, r0 = l, r
while l < r:
if l & 1:
self.apply(l, value)
l += 1
if r & 1:
r -= 1
self.apply(r, value)
l >>= 1
r >>= 1
self.build(l0)
self.build(r0 - 1)
def query(self, l, r):
if l >= r:
return -float('inf')
t = self.t
l += self.N
r += self.N
self.push(l)
self.push(r - 1)
res = -float('inf')
while l < r:
if l & 1:
res = max(res, t[l])
l += 1
if r & 1:
r -= 1
res = max(t[r], res)
l >>= 1
r >>= 1
return res
n, m, p = map(int, input().split())
weapon = []
for _ in range(n):
a, ca = map(int, input().split())
weapon.append((a, ca))
defense = []
for _ in range(m):
b, cb = map(int, input().split())
defense.append((b, cb))
monster = []
for _ in range(p):
x, y, z = map(int, input().split())
monster.append((x, y, z))
weapon.sort(key=itemgetter(0))
defense.sort(key=itemgetter(0))
monster.sort(key=itemgetter(0))
st = SegmTree(m)
N = st.N
t = st.t
for i, (b, cb) in enumerate(defense):
t[i + N] = -cb
st.rebuild()
i = 0
maxScore = -float('inf')
for a, ca in weapon:
st.inc(0, m, -ca)
while i < p and monster[i][0] < a:
x, y, z = monster[i]
goodDef = bisect_right(defense, (y + 1, 0))
st.inc(goodDef, m, z)
i += 1
currScore = st.query(0, m)
maxScore = max(maxScore, currScore)
st.inc(0, m, ca)
print(maxScore)
``` | output | 1 | 96,254 | 2 | 192,509 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1 | instruction | 0 | 96,255 | 2 | 192,510 |
Tags: brute force, data structures, sortings
Correct Solution:
```
# quick input by @c1729 and @pajenegod
import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
from bisect import bisect_right
from operator import itemgetter
class SegmTree:
'''
- increment on interval
- get max on interval
'''
def __init__(self, size):
N = 1
h = 0
while N < size:
N <<= 1
h += 1
self.N = N
self.h = h
self.t = [0] * (2 * N)
self.d = [0] * N
def apply(self, p, value):
self.t[p] += value
if p < self.N:
self.d[p] += value
def build(self, p):
t = self.t
d = self.d
while p > 1:
p >>= 1
t[p] = max(t[p<<1], t[p<<1|1]) + d[p]
def rebuild(self):
t = self.t
for p in reversed(range(1, self.N)):
t[p] = max(t[p<<1], t[p<<1|1])
def push(self, p):
d = self.d
for s in range(self.h, 0, -1):
i = p >> s
if d[i] != 0:
self.apply(i<<1, d[i])
self.apply(i<<1|1, d[i])
d[i] = 0
def inc(self, l, r, value):
if l >= r:
return
l += self.N
r += self.N
l0, r0 = l, r
while l < r:
if l & 1:
self.apply(l, value)
l += 1
if r & 1:
r -= 1
self.apply(r, value)
l >>= 1
r >>= 1
self.build(l0)
self.build(r0 - 1)
def query(self, l, r):
if l >= r:
return -float('inf')
t = self.t
l += self.N
r += self.N
self.push(l)
self.push(r - 1)
res = -float('inf')
while l < r:
if l & 1:
res = max(res, t[l])
l += 1
if r & 1:
r -= 1
res = max(t[r], res)
l >>= 1
r >>= 1
return res
n, m, p = map(int, input().split())
weapon = []
for _ in range(n):
a, ca = map(int, input().split())
# a, ca = n - _, n - _
weapon.append((a, ca))
defense = []
for _ in range(m):
b, cb = map(int, input().split())
# b, cb = m - _, m - _
defense.append((b, cb))
monster = []
for _ in range(p):
x, y, z = map(int, input().split())
# x, y, z = p - _, p - _, p - _
monster.append((x, y, z))
weapon.sort(key=itemgetter(0))
defense.sort(key=itemgetter(0))
monster.sort(key=itemgetter(0))
# store score of each defense item
st = SegmTree(m)
N, t = st.N, st.t
for i, (b, cb) in enumerate(defense):
t[i + N] = -cb
st.rebuild()
i = 0
maxScore = -float('inf')
for a, ca in weapon:
st.inc(0, m, -ca)
while i < p and monster[i][0] < a:
x, y, z = monster[i]
goodDef = bisect_right(defense, (y + 1, 0))
st.inc(goodDef, m, z)
i += 1
currScore = st.query(0, m)
maxScore = max(maxScore, currScore)
st.inc(0, m, ca)
print(maxScore)
``` | output | 1 | 96,255 | 2 | 192,511 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1
Submitted Solution:
```
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 10**9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split(' ')]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n').split(' ')]
def li3():return [int(i) for i in input().rstrip('\n')]
def makel(l):
helpl = []
for i in l:
while len(helpl) and helpl[-1][-1] >= i[1]:helpl.pop()
helpl.append(i)
return helpl
def do(l3):
ans = [-1]
tot = 0
atmin = -float('inf')
demin = -float('inf')
for i in range(p):
tot += l3[i][-1]
atmin = max(atmin,l3[i][0] + 1)
demin = max(demin,l3[i][1] + 1)
curra = bl(l1,atmin)
if curra == n:break
else:
curra = a[curra]
currb = bl(l2,demin)
if currb == m:break
else:
currb = b[currb]
ans.append(tot - curra - currb)
return max(ans)
n,m,p = li()
l1 = []
l2 = []
a = []
b = []
l3 = []
for i in range(n):
l1.append(li())
l1.sort()
l1 = makel(l1)
n = len(l1)
for i in range(n):a.append(l1[i][1])
l1 = [i[0] for i in l1]
for i in range(m):
l2.append(li())
l2.sort()
l2 = makel(l2)
m = len(l2)
for i in range(m):b.append(l2[i][1])
l2 = [i[0] for i in l2]
ans = -float('inf')
for i in range(p):l3.append(li())
l3.sort()
ans = do(l3)
l3.sort(key = lambda x:(x[1],x[0]))
ans = max(ans,do(l3[:]))
l3.sort(key = lambda x:(x[2],x[0],x[1]))
ans = max(ans,do(l3[:]))
l3.sort(key = lambda x:(x[2],x[1],x[0]))
ans = max(ans,do(l3))
print(ans)
``` | instruction | 0 | 96,256 | 2 | 192,512 |
No | output | 1 | 96,256 | 2 | 192,513 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1
Submitted Solution:
```
import sys
readline = sys.stdin.readline
N, M, P = map(int, readline().split())
xcx = [tuple(map(int, readline().split())) for _ in range(N)]
ycy = [tuple(map(int, readline().split())) for _ in range(M)]
enemy = [tuple(map(int, readline().split())) for _ in range(P)]
JM = 10**6+4
table = [-2147483648]*JM
event = [[] for _ in range(JM)]
for x, cx in xcx:
event[x].append((0, cx))
for y, cy in ycy:
table[y] = max(table[y], -cy)
for x, y, z in enemy:
event[x+1].append((y+1, z))
####
N0 = 1<<20
data = [-2147483648]*N0 + table + [-2147483648]*(N0 - JM)
lazy = [0]*(2*N0)
for i in range(N0-1, 0, -1):
data[i] = max(data[2*i], data[2*i+1])
def addN0(l, x):
L = l+N0
R = 2*N0
Li = L//(L & -L)//2
while L < R :
if L & 1:
data[L] += x
lazy[L] += x
L += 1
L >>= 1
R >>= 1
while Li:
data[Li] = max(data[2*Li], data[2*Li+1]) + lazy[Li]
Li >>= 1
####
ans = -2147483648
for i in range(JM):
for y, z in event[i]:
if y:
addN0(y, z)
else:
ans = max(ans, data[1] - z)
print(ans)
``` | instruction | 0 | 96,257 | 2 | 192,514 |
No | output | 1 | 96,257 | 2 | 192,515 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1
Submitted Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
if __name__ == '__main__':
n,m,p = [int(x) for x in input().split()]
weapon=[]
weaponprice=[]
armor=[]
armorprice=[]
monster=[]
for i in range(n):
newW,newWP=[int(x) for x in input().split()]
weapon.append(newW)
weaponprice.append(newWP)
for i in range(m):
newA,newAP=[int(x) for x in input().split()]
armor.append(newA)
armorprice.append(newAP)
for i in range(p):
newM=[int(x) for x in input().split()]
monster.append(newM)
currWi,currAi=weaponprice.index(min(weaponprice)),armorprice.index(min(armorprice))
currW,currA=weapon[currWi],armor[currAi]
#print(armorprice,currAi)
currProfit=0
for i in range(p):
#print(i,currAi,currWi)
if monster[i][0]<currW and monster[i][1]<currA:
currProfit+=monster[i][2]
else:
profitGain=monster[i][2]
dmg,defense=False,False
if monster[i][0]>=currW:
if currWi<n-i:
dmg=weapon[currWi+1]
costW=weaponprice[currWi+1]
if monster[i][1]>=currA:
if currAi<n-i:
defense=armor[currAi+1]
costA=armorprice[currAi+1]
#print(dmg,defense,monster[i])
if dmg>monster[i][0] and defense>monster[i][1]:
if costW<=costA and (costW-weaponprice[currWi])<profitGain:
currProfit+=(monster[i][2])
currWi+=1
currW=dmg
elif costW>=costA and (costA-armorprice[currAi])<profitGain:
currProfit+=(monster[i][2])
currAi+=1
currA=defense
else:
continue
elif dmg>monster[i][0]:
if (costW-weaponprice[currWi])<profitGain:
currProfit+=(monster[i][2])
currWi+=1
currW=dmg
else:
continue
elif defense>monster[i][1]:
if (costA-armorprice[currAi])<profitGain:
currProfit+=(monster[i][2])
currAi+=1
currA=defense
else:
continue
else:
continue
print(currProfit,currWi,currAi)
print(currProfit-weaponprice[currWi]-armorprice[currAi])
``` | instruction | 0 | 96,258 | 2 | 192,516 |
No | output | 1 | 96,258 | 2 | 192,517 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Roma is playing a new expansion for his favorite game World of Darkraft. He made a new character and is going for his first grind.
Roma has a choice to buy exactly one of n different weapons and exactly one of m different armor sets. Weapon i has attack modifier a_i and is worth ca_i coins, and armor set j has defense modifier b_j and is worth cb_j coins.
After choosing his equipment Roma can proceed to defeat some monsters. There are p monsters he can try to defeat. Monster k has defense x_k, attack y_k and possesses z_k coins. Roma can defeat a monster if his weapon's attack modifier is larger than the monster's defense, and his armor set's defense modifier is larger than the monster's attack. That is, a monster k can be defeated with a weapon i and an armor set j if a_i > x_k and b_j > y_k. After defeating the monster, Roma takes all the coins from them. During the grind, Roma can defeat as many monsters as he likes. Monsters do not respawn, thus each monster can be defeated at most one.
Thanks to Roma's excessive donations, we can assume that he has an infinite amount of in-game currency and can afford any of the weapons and armor sets. Still, he wants to maximize the profit of the grind. The profit is defined as the total coins obtained from all defeated monsters minus the cost of his equipment. Note that Roma must purchase a weapon and an armor set even if he can not cover their cost with obtained coins.
Help Roma find the maximum profit of the grind.
Input
The first line contains three integers n, m, and p (1 β€ n, m, p β€ 2 β
10^5) β the number of available weapons, armor sets and monsters respectively.
The following n lines describe available weapons. The i-th of these lines contains two integers a_i and ca_i (1 β€ a_i β€ 10^6, 1 β€ ca_i β€ 10^9) β the attack modifier and the cost of the weapon i.
The following m lines describe available armor sets. The j-th of these lines contains two integers b_j and cb_j (1 β€ b_j β€ 10^6, 1 β€ cb_j β€ 10^9) β the defense modifier and the cost of the armor set j.
The following p lines describe monsters. The k-th of these lines contains three integers x_k, y_k, z_k (1 β€ x_k, y_k β€ 10^6, 1 β€ z_k β€ 10^3) β defense, attack and the number of coins of the monster k.
Output
Print a single integer β the maximum profit of the grind.
Example
Input
2 3 3
2 3
4 7
2 4
3 2
5 11
1 2 4
2 1 6
3 4 6
Output
1
Submitted Solution:
```
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
if __name__ == '__main__':
n,m,p = [int(x) for x in input().split()]
weapon=[]
weaponprice=[]
armor=[]
armorprice=[]
monster=[]
for i in range(n):
newW,newWP=[int(x) for x in input().split()]
weapon.append(newW)
weaponprice.append(newWP)
for i in range(m):
newA,newAP=[int(x) for x in input().split()]
armor.append(newA)
armorprice.append(newAP)
for i in range(p):
newM=[int(x) for x in input().split()]
monster.append(newM)
currWi,currAi=weaponprice.index(min(weaponprice)),armorprice.index(min(armorprice))
currW,currA=weapon[currWi],armor[currAi]
#print(armorprice,currAi)
currProfit=0
for i in range(p):
#print(i,currAi,currWi)
if monster[i][0]<currW and monster[i][1]<currA:
currProfit+=monster[i][2]
else:
profitGain=monster[i][2]
dmg,defense=False,False
if monster[i][0]>=currW:
if currWi<n-i:
dmg=weapon[currWi+1]
costW=weaponprice[currWi+1]
if monster[i][1]>=currA:
if currAi<n-i:
defense=armor[currAi+1]
costA=armorprice[currAi+1]
if dmg and defense:
if costW<=costA and (costW-weaponprice[currWi])<profitGain:
currProfit+=(monster[i][2])
currWi+=1
currW=dmg
elif costW>=costA and (costA-armorprice[currAi])<profitGain:
currProfit+=(monster[i][2])
currAi+=1
currA=defense
else:
break
elif dmg:
if (costW-weaponprice[currWi])<profitGain:
currProfit+=(monster[i][2])
currWi+=1
currW=dmg
else:
break
elif defense:
if (costA-armorprice[currAi])<profitGain:
currProfit+=(monster[i][2])
currAi+=1
currA=defense
else:
break
else:
break
print(currProfit-weaponprice[currWi]-armorprice[currAi])
``` | instruction | 0 | 96,259 | 2 | 192,518 |
No | output | 1 | 96,259 | 2 | 192,519 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,711 | 2 | 193,422 |
Tags: greedy, sortings
Correct Solution:
```
# python3
from sys import stdin
def main():
def parseints(line): return tuple(map(int, line.split()))
def bdiff(creature): return max(0, creature[0] - creature[1])
n, a, b = parseints(input())
hand = list(map(parseints, stdin.readlines()))
ans = sum(creature[1] for creature in hand) # default damage
if b:
hand.sort(key=bdiff)
best = 0
if n > b:
lost = bdiff(hand[n - b])
for creature in hand[:n-b]:
best = max(best, (creature[0] << a) - creature[1] - lost)
for creature in hand[max(0,n-b):]:
best = max(best, (creature[0] << a) - max(creature))
ans += bdiff(creature)
ans += best
print(ans)
main()
``` | output | 1 | 96,711 | 2 | 193,423 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,712 | 2 | 193,424 |
Tags: greedy, sortings
Correct Solution:
```
# coding=utf-8
from sys import stdin
rd = lambda l: tuple(map(int, l.split()))
n, a, b = rd(input())
b = min(n, b)
s = list(map(rd, stdin.readlines()))
f = lambda x:max(0, x[0]-x[1])
g = lambda x:(x[0]<<a)-x[1]
ans = sum(x[1] for x in s)
mid = 0
if b:
s.sort(key=f, reverse=True)
t = sum(f(x) for x in s[:b] )
for i in range(b):
mid = max(mid, t-f(s[i])+g(s[i]))
for i in range(b, n):
mid = max(mid, t-f(s[b-1])+g(s[i]))
ans += mid
print(ans)
``` | output | 1 | 96,712 | 2 | 193,425 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,713 | 2 | 193,426 |
Tags: greedy, sortings
Correct Solution:
```
# python3
from sys import stdin
from collections import namedtuple
def readline(): return tuple(map(int, input().split()))
n, a, b = readline()
hand = [tuple(map(int, line.split())) for line in stdin.readlines()]
if not b:
print(sum(creature[1] for creature in hand))
else:
hand.sort(key=lambda self: self[0] - self[1])
best = 0
if n > b:
l = hand[n - b]
lost = max(0, l[0] - l[1])
for creature in hand[:n-b]:
best = max(best, (creature[0] << a) - creature[1] - lost)
for creature in hand[max(0,n-b):]:
best = max(best, (creature[0] << a) - max(creature))
print(sum(creature[1] for creature in hand)
+ sum(max(0, creature[0] - creature[1]) for creature in hand[max(0,n-b):])
+ best)
``` | output | 1 | 96,713 | 2 | 193,427 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,714 | 2 | 193,428 |
Tags: greedy, sortings
Correct Solution:
```
import sys
n, a, b = map(int, sys.stdin.buffer.readline().decode('utf-8').split())
creature = [list(map(int, line.decode('utf-8').split()))
for line in sys.stdin.buffer]
creature.sort(key=lambda x: -x[0]+x[1])
if b == 0:
print(sum(x for _, x in creature))
exit()
base = 0
for i in range(min(n, b)):
base += max(creature[i])
for i in range(min(n, b), n):
base += creature[i][1]
ans = base
mul = 1 << a
for i in range(min(n, b)):
ans = max(
ans,
base - max(creature[i]) + creature[i][0] * mul
)
base = base - max(creature[min(n, b)-1]) + creature[min(n, b)-1][1]
for i in range(min(n, b), n):
ans = max(
ans,
base - creature[i][1] + creature[i][0] * mul
)
print(ans)
``` | output | 1 | 96,714 | 2 | 193,429 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,715 | 2 | 193,430 |
Tags: greedy, sortings
Correct Solution:
```
def well_played():
n,a,b = [int(x) for x in input().split()]
p = [(0,0)] * n
b= min(b,n)
for i in range(n):
h,d =[int(x) for x in input().split()]
p[i] =(h,d)
p.sort(key=lambda x:x[0]-x[1],reverse=True)
s=0
for i in range(b):
s+=max(p[i][0],p[i][1])
for i in range(b,n):
s+= p[i][1]
ans = s
for i in range(b):
ans = max(ans,s - max(p[i][0],p[i][1]) + ((p[i][0]) <<a) )
s=s - max(p[b-1][0],p[b-1][1]) + p[b-1][1]
if(b):
for i in range(b,n):
ans= max(ans,s - p[i][1] + ((p[i][0]) <<a) )
print(ans)
well_played()
``` | output | 1 | 96,715 | 2 | 193,431 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,716 | 2 | 193,432 |
Tags: greedy, sortings
Correct Solution:
```
import sys
read=lambda:map(int,sys.stdin.buffer.readline().split())
n,a,b=read()
v=[tuple(read()) for _ in range(n)]
ans=0
if b>0:
c=[v[x][0]-v[x][1] for x in range(n)]
w,r=list(range(n)),[0]*n
w.sort(key=lambda x:c[x],reverse=True)
for i in range(n): r[w[i]]=i
f=True;s=0;m=min(n,b)
for i in range(m):
k=c[w[i]]
if k<=0: f=False;m=i;break
s+=k
ans=s
if a>0:
for i in range(n):
k=v[i][0]*(1<<a)-v[i][1]
tmp=s+k
if r[i]<m:
tmp-=c[i]
elif f:
tmp-=c[w[m-1]]
if tmp>ans: ans=tmp
print(sum([v[i][1] for i in range(n)])+ans)
``` | output | 1 | 96,716 | 2 | 193,433 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,717 | 2 | 193,434 |
Tags: greedy, sortings
Correct Solution:
```
def main():
n, a, b = map(int, input().split())
arr = [0 for _ in range(n)]
brr = [0 for _ in range(n)]
for i in range(n):
arr[i], brr[i] = map(int,input().split())
p = [i for i in range(n)]
p.sort(key=lambda x: arr[x]-brr[x], reverse=True)
total = 0
for i in range(len(arr)):
if i < b:
total += max(arr[p[i]], brr[p[i]])
else:
total += brr[p[i]]
if b == 0:
print(total)
return
s = total
pp = 1 << a
for i in range(n):
ctotal = s
if i < b:
ctotal -= max(arr[p[i]], brr[p[i]])
ctotal += arr[p[i]] * pp
else:
ctotal -= brr[p[i]]
ctotal += arr[p[i]] * pp
ctotal -= max(arr[p[b-1]], brr[p[b-1]])
ctotal += brr[p[b-1]]
total = max(total, ctotal)
print(total)
main()
``` | output | 1 | 96,717 | 2 | 193,435 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26. | instruction | 0 | 96,718 | 2 | 193,436 |
Tags: greedy, sortings
Correct Solution:
```
# python3
from sys import stdin
def readline(): return tuple(map(int, input().split()))
def bdiff(creature): return max(0, creature[0] - creature[1])
n, a, b = readline()
hand = [tuple(map(int, line.split())) for line in stdin.readlines()]
ans = sum(creature[1] for creature in hand) # default damage
if b:
hand.sort(key=bdiff)
best = 0
if n > b:
lost = bdiff(hand[n - b])
for creature in hand[:n-b]:
best = max(best, (creature[0] << a) - creature[1] - lost)
for creature in hand[max(0,n-b):]:
best = max(best, (creature[0] << a) - max(creature))
ans += bdiff(creature)
ans += best
print(ans)
``` | output | 1 | 96,718 | 2 | 193,437 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
Submitted Solution:
```
from sys import stdin
rd = lambda l: tuple(map(int, l.split()))
n, a, b = rd(input())
b = min(b, n)
p = list(map(rd, stdin.readlines()))
f = lambda x: max(0, x[0] - x[1])
g = lambda x: (x[0] << a) - x[1]
s = sum(x[1] for x in p)
e = 0
if b:
p.sort(key=f, reverse=True)
t = sum(f(x) for x in p[:b])
for i in range(b):
e = max(e, t - f(p[i]) + g(p[i]))
for i in range(b, n):
e = max(e, t - f(p[b - 1]) + g(p[i]))
s += e
print(s)
``` | instruction | 0 | 96,719 | 2 | 193,438 |
Yes | output | 1 | 96,719 | 2 | 193,439 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
Submitted Solution:
```
import sys
read=lambda:map(int,sys.stdin.buffer.readline().split())
n,a,b=read()
v=[tuple(read()) for _ in range(n)]
ans=0
if b>0:
c=[v[x][0]-v[x][1] for x in range(n)]
w,r=list(range(n)),[0]*n
w.sort(key=lambda x:c[x],reverse=True)
for i in range(n): r[w[i]]=i
f=True;s=0;m=min(n,b)
for i in range(m):
k=c[w[i]]
if k<=0: f=False;m=i;break
s+=k
ans=s
if a>0:
for i in range(n):
k=v[i][0]*(1<<a)-v[i][1];tmp=s+k
if r[i]<m: tmp-=c[i]
elif f: tmp-=c[w[m-1]]
if tmp>ans: ans=tmp
print(sum([v[i][1] for i in range(n)])+ans)
``` | instruction | 0 | 96,720 | 2 | 193,440 |
Yes | output | 1 | 96,720 | 2 | 193,441 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
Submitted Solution:
```
def main():
n, a, b = map(int, input().split())
arr = []
ind = -1
curr = -10000000000
for i in range(n):
arr.append(list(map(int,input().split())))
if arr[-1][0] * 2 ** a - arr[-1][1] > curr:
ind = i
curr = arr[-1][0] * 2 ** a - arr[-1][1]
arr[ind][0] *= 2 ** a
arr.sort(key=lambda x:-x[0]+x[1])
total = 0
for i in arr:
if b > 0 and i[0] > i[1]:
total += i[0]
b -= 1
else:
total += i[1]
print(total)
main()
``` | instruction | 0 | 96,721 | 2 | 193,442 |
No | output | 1 | 96,721 | 2 | 193,443 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
Submitted Solution:
```
n, a, b=map(int, input().split(' '));
hp=[0]*n;
dmg=[0]*n;
for i in range(n):
hp[i], dmg[i]=map(int, input().split(' '));
sumdmg=sum(dmg);
result=sumdmg;
koef=2**a;
if a>0:
bonusDmg=[max(hp[j]*koef-dmg[j], 0) - max(hp[j]-dmg[j], 0) for j in range(n)];
iMax=0;
maxBonus=0;
for i in range(n):
if bonusDmg[i]>maxBonus:
maxBonus=bonusDmg[i];
iMax=i;
#print(bonusDmg, iMax)
else:
iMax=-1;
#diff - ΠΏΡΠΈΠΎΡΡΡ Π΄Π°ΠΌΠ°Π³Π° ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ Π·Π°ΠΊΠ»ΠΈΠ½Π°Π½ΠΈΡ
diff=[max(hp[j]-dmg[j], 0) for j in range(n) if j!=iMax];
diff.sort();
if iMax!=-1:
b-=1;
n-=1;
result+=max(hp[iMax]*koef-dmg[iMax], 0);
for j in range(n-1, max(-1, n-b-1), -1):
result+=diff[j];
if diff[j]==0:
break;
print(result);
``` | instruction | 0 | 96,722 | 2 | 193,444 |
No | output | 1 | 96,722 | 2 | 193,445 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
Submitted Solution:
```
import heapq
n,a,b = map(int, input().split())
L = [[0]*2 for i in range(n)]
for i in range(n):
L[i][0],L[i][1] = map(int, input().split())
a = 2**a
if b == 0:
print(sum([x[1] for x in L]))
else:
XX = L[0][0]*a-L[0][1]
XXX = 0
R = []
for i in range(n):
if L[i][0]*a-L[i][1]>XX:
XX = L[i][0]*a-L[i][1]
XXX = i
if L[i][0]>L[i][1]:
R.append(i)
#print(XX,XXX)
if L[XXX][0]*a-L[XXX][1]<=0:
print(sum([x[1] for x in L]))
else:
L[XXX] = [L[XXX][0]*a,L[XXX][0]*a]
b-=1
S = sum([x[1] for x in L])
# L.sort(key=lambda x:x[0]-x[1],reverse=True)
# #print(L)
# i = 0
# while i<b and i<n and L[i][1]<L[i][0]:
# L[i][1] = L[i][0]
# i+=1
# print(sum([x[1] for x in L]))
LL = []
count = 0
#print(R)
for i in R:
if count<b:
heapq.heappush(LL,L[i][0]-L[i][1])
count+=1
else:
heapq.heappushpop(LL,L[i][0]-L[i][1])
print(S+sum(LL))
``` | instruction | 0 | 96,723 | 2 | 193,446 |
No | output | 1 | 96,723 | 2 | 193,447 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Recently Max has got himself into popular CCG "BrainStone". As "BrainStone" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:
Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock:
1. Doubles health of the creature (hpi := hpiΒ·2);
2. Assigns value of health of the creature to its damage (dmgi := hpi).
Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.
Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
Input
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.
The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
Output
Print single integer β maximum total damage creatures can deal.
Examples
Input
2 1 1
10 15
6 1
Output
27
Input
3 0 3
10 8
7 11
5 2
Output
26
Note
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.
In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
Submitted Solution:
```
n, a, b=map(int, input().split(' '));
hp=[0]*n;
dmg=[0]*n;
for i in range(n):
hp[i], dmg[i]=map(int, input().split(' '));
sumdmg=sum(dmg);
result=sumdmg;
koef=2**a;
if a>0:
bonusDmg=[max(hp[j]*koef-dmg[j], 0) - max(hp[j]-dmg[j], 0) for j in range(n)];
iMax=0;
maxBonus=0;
for i in range(n):
if bonusDmg[i]>maxBonus:
maxBonus=bonusDmg[i];
iMax=i;
print(bonusDmg, iMax)
else:
iMax=-1;
#diff - ΠΏΡΠΈΠΎΡΡΡ Π΄Π°ΠΌΠ°Π³Π° ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ Π·Π°ΠΊΠ»ΠΈΠ½Π°Π½ΠΈΡ
diff=[hp[j]-dmg[j] for j in range(n) if j!=iMax and hp[j]-dmg[j]>0];
diff.sort();
if iMax!=-1:
b-=1;
result+=max(hp[iMax]*koef-dmg[iMax], 0);
n=len(diff);
for j in range(n-1, max(-1, n-b-1), -1):
result+=diff[j];
if diff[j]==0:
break;
print(result);
``` | instruction | 0 | 96,724 | 2 | 193,448 |
No | output | 1 | 96,724 | 2 | 193,449 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,314 | 2 | 194,628 |
Tags: dp, flows, greedy
Correct Solution:
```
n, m = map(int, input().split())
(a, d) = ([], [])
for i in range(n):
t, val = input().split()
(a if t == 'ATK' else d).append(int(val))
my = sorted([int(input()) for i in range(m)])
a.sort()
d.sort()
def solve1():
ret = 0
used = [False] * m
for val in d:
for i in range(m):
if not used[i] and my[i] > val:
used[i] = True
break
else:
return 0
for val in a:
for i in range(m):
if not used[i] and my[i] >= val:
used[i] = True
ret += my[i] - val
break
else:
return 0
return ret + sum([my[i] for i in range(m) if not used[i]])
def solve2():
ret = 0
for k in range(min(len(a), m)):
if my[-k-1] >= a[k]: ret += my[-k-1] - a[k]
else: break
return ret
print(max(solve1(), solve2()))
``` | output | 1 | 97,314 | 2 | 194,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,315 | 2 | 194,630 |
Tags: dp, flows, greedy
Correct Solution:
```
import sys
n, m = map(int, input().split())
atk = []
dfs = []
for _ in range(n):
t, s = input().split()
(atk if t == "ATK" else dfs).append(int(s))
atk = sorted(atk)
dfs = sorted(dfs)
mine = sorted([int(input()) for _ in range(m)])
def s1():
ret = 0
done = [False] * m
for s in dfs:
check = False
for i in range(m):
if not done[i] and mine[i] > s:
check = True
done[i] = True
break
if not check:
return 0
for s in atk:
check = False
for i in range(m):
if not done[i] and mine[i] >= s:
check = True
done[i] = True
ret += mine[i] - s
break
if not check:
return 0
for i in range(m):
if not done[i]:
ret += mine[i]
return ret
def s2():
ret = 0
for i in range(m):
alc = 0
for j in range(min(m - i, len(atk))):
if mine[i + j] < atk[j]:
break
else:
alc += mine[i + j] - atk[j]
ret = max(ret, alc)
return ret
print(max(s1(), s2()))
``` | output | 1 | 97,315 | 2 | 194,631 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,316 | 2 | 194,632 |
Tags: dp, flows, greedy
Correct Solution:
```
n, m = map(int, input().split())
u = [[], []]
for q in range(n):
p, s = input().split()
u[p == 'ATK'].append(int(s))
d, a = [sorted(q) for q in u]
v = sorted(int(input()) for q in range(m))
k, s = 0, sum(v)
i = j = 0
for q in v:
if i < len(d) and q > d[i]:
s -= q
i += 1
elif j < len(a) and q >= a[j]:
s -= a[j]
j += 1
if i + j - len(a) - len(d): s = 0
for q in v:
if k < len(a) and q >= a[k]: k += 1
x = y = 0
v.reverse()
for i in range(k):
x += a[i]
y += v[i]
s = max(s, y - x)
print(s)
``` | output | 1 | 97,316 | 2 | 194,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,317 | 2 | 194,634 |
Tags: dp, flows, greedy
Correct Solution:
```
n , m = map(int , input().split())
a , d = [1e9] , [1e9]
for x in range(n) :
p , s = input().split()
[d , a][p < 'B'].append(int(s))
v = [int(input()) for y in range(m) ]
for q in [a , d , v] : q.sort()
s = sum(v)
i = j = 0
for t in v :
if t > d[i] : s , i = s - t , i + 1
elif t >= a[j] : s , j = s - a[j] , j + 1
if i + j - n : s = 0
print(max(s , sum(max(0 , y - x) for x , y in zip(a, v[::-1]))))
``` | output | 1 | 97,317 | 2 | 194,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,318 | 2 | 194,636 |
Tags: dp, flows, greedy
Correct Solution:
```
n, m = map(int, input().split())
(a, d) = ([], [])
for i in range(n):
t, val = input().split()
(a if t == 'ATK' else d).append(int(val))
my = sorted([int(input()) for i in range(m)])
a.sort()
d.sort()
def solve1():
ret = 0
used = [False] * m
for val in d:
for i in range(m):
if not used[i] and my[i] > val:
used[i] = True
break
else:
return 0
for val in a:
for i in range(m):
if not used[i] and my[i] >= val:
used[i] = True
ret += my[i] - val
break
else:
return 0
return ret + sum([my[i] for i in range(m) if not used[i]])
def solve2():
ret = 0
my.reverse()
for k in range(min(len(a), m)):
if my[k] >= a[k]: ret += my[k] - a[k]
else: break
return ret
print(max(solve1(), solve2()))
``` | output | 1 | 97,318 | 2 | 194,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,319 | 2 | 194,638 |
Tags: dp, flows, greedy
Correct Solution:
```
n, m = map(int, input().split())
(a, d, my) = ([], [], [])
for i in range(n):
t, val = input().split()
(a if t == 'ATK' else d).append(int(val))
my = sorted([int(input()) for i in range(m)])
a.sort()
d.sort()
def solve1():
ret = 0
used = [False] * m
for val in d:
for i in range(m):
if not used[i] and my[i] > val:
used[i] = True
break
else:
return 0
for val in a:
for i in range(m):
if not used[i] and my[i] >= val:
used[i] = True
ret += my[i] - val
break
else:
return 0
ret += sum([my[i] for i in range(m) if not used[i]])
return ret
def solve2():
ret = 0
my.reverse()
for k in range(0, min(len(a), m)):
if my[k] >= a[k]: ret += my[k] - a[k]
else: break
return ret
print(max(solve1(), solve2()))
``` | output | 1 | 97,319 | 2 | 194,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,320 | 2 | 194,640 |
Tags: dp, flows, greedy
Correct Solution:
```
n, m = map(int, input().split())
(a, d) = ([], [])
for i in range(n):
t, val = input().split()
(a if t == 'ATK' else d).append(int(val))
my = sorted([int(input()) for i in range(m)])
a.sort()
d.sort()
def solve1():
ret = 0
used = [False] * m
for val in d:
for i in range(m):
if not used[i] and my[i] > val:
used[i] = True
break
else:
return 0
for val in a:
for i in range(m):
if not used[i] and my[i] >= val:
used[i] = True
ret += my[i] - val
break
else:
return 0
return ret + sum([my[i] for i in range(m) if not used[i]])
def solve2():
ret = 0
for k in range(min(len(a), m)):
if my[-k-1] >= a[k]: ret += my[-k-1] - a[k]
else: break
return ret
print(max(solve1(), solve2()))
# Made By Mostafa_Khaled
``` | output | 1 | 97,320 | 2 | 194,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | instruction | 0 | 97,321 | 2 | 194,642 |
Tags: dp, flows, greedy
Correct Solution:
```
def avoiddef(a,b):
b.reverse()
x = 0
i = 0
n = min(len(a),len(b))
while i<n and b[i]>a[i]:
x += b[i]-a[i]
i += 1
return x
def killdefs(d,a,b):
i = 0
n = len(b)
b2 = [bb for bb in b]
for dd in d:
while i<n and b2[i]<=dd:
i += 1
if i==n: return -1 #fail to kill all defs!
b2[i]= 0
i += 1
b2.sort()
x = 0
i = 0
for aa in a:
while i<n and b2[i]<aa:
i += 1
if i==n: return -1 #failed to kill all atks!
i += 1
return sum(b2)-sum(a)
def f(a,bl):
al = [int(c[1]) for c in a if c[0]=='ATK']
dl = [int(c[1]) for c in a if c[0]=='DEF']
al.sort()
dl.sort()
bl.sort()
return max(killdefs(dl,al,bl),avoiddef(al,bl))
n,m = list(map(int,input().split()))
a = [input().split() for _ in range(n)]
b = [int(input()) for _ in range(m)]
print(f(a,b))
``` | output | 1 | 97,321 | 2 | 194,643 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
Submitted Solution:
```
n,m = map(int,input().split())
jiro = []
for i in range(n):
tmp=input().split()
tmp[1]=int(tmp[1])
jiro.append(tmp)
ciel = []
for i in range(m):
ciel.append(int(input()))
a,b,c=0,0,0
#case #1: All-in
ciel.sort(reverse=True)
jiro.sort(key=lambda x:x[0])
jiro.sort(key=lambda x:x[1])
sm = 0
ji = 0
for i in ciel:
if i > jiro[ji][1]:
sm += i - jiro[ji][1]
else:
break
ji += 1
if ji >= len(jiro) or jiro[ji][0] == 'DEF':
break
a=(sm)
#case #1.5: Defeat all
jiro.sort(key=lambda x:x[1],reverse=True)
sm = 0
ji = 0
for i in (i for i in jiro if i[0] != 'DEF'):
if i[1] < ciel[ji]:
sm += ciel[ji] - i[1]
else:
break
ji += 1
if ji >= len(ciel):
break
b=(sm)
#case #2: Defeat all
sm = 0
ji = 0
fst =0
for i in (i for i in jiro if i[0] == 'DEF'):
x=-1
for j in range(len(ciel)):
if ciel[j] > i[1]:
x=j
break
if x==-1:
fst=1
break
del ciel[x]
if fst ==0:
for i in (i for i in jiro if i[0] != 'DEF'):
if i[1] < ciel[ji]:
sm += ciel[ji] - i[1]
del ciel[ji]
else:
fst=1
break
if len(ciel)<=0:
fst=1
break
if fst ==0:
sm += sum(ciel)
c=(sm)
print(max(a,b,c))
``` | instruction | 0 | 97,322 | 2 | 194,644 |
No | output | 1 | 97,322 | 2 | 194,645 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
Submitted Solution:
```
n,m=map(int,input().split())
ja=list()
jd=list()
for i in range(n):
p,s=input().split()
s=int(s)
if(p=="ATK"):
ja.append(s)
else:
jd.append(s)
ca=list(int(input()) for i in range(m))
ja.sort()
jd.sort()
ca.sort()
ca2=ca[:]
w=False
for i in jd:
for j in range(len(ca)):
if(ca[j]>i):
ca.pop(j)
w=True
break
if not w:
break
d=0
if(w):
ja.reverse()
ca.reverse()
for i in range(len(ja)):
if(ja[i]>ca[i]):
w=False
break
if(w):
print(sum(ca)-sum(ja))
else:
ca=ca2[:]
ca2.reverse()
d=0
n=0
for i in range(len(ca2)):
if(ca2[i]>=ja[i]):
d+=ca2[i]-ja[i]
n+=1
else:
break
if(n==len(ja)):
d=sum(ca2)-sum(ja)
print(d)
``` | instruction | 0 | 97,323 | 2 | 194,646 |
No | output | 1 | 97,323 | 2 | 194,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
Submitted Solution:
```
n, m = map(int, input().split())
u = [[], []]
for i in range(n):
p, s = input().split()
u[p == 'ATK'].append(int(s))
d, a = [sorted(q) for q in u]
v = sorted(int(input()) for q in range(m))
s = k = 0
for q in v:
if k < len(a) and q >= a[k]: k += 1
x = y = 0
v.reverse()
for i in range(k):
x += a[i]
y += v[i]
s = max(s, y - x)
t = sum(v)
i = j = 0
for q in v:
if i < len(d) and q > d[i]:
t -= q
i += 1
elif j < len(a) and q >= a[j]:
t -= a[j]
j += 1
if i + j - len(a) - len(d): t = 0
print(max(s, t))
``` | instruction | 0 | 97,324 | 2 | 194,648 |
No | output | 1 | 97,324 | 2 | 194,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Fox Ciel is playing a card game with her friend Jiro.
Jiro has n cards, each one has two attributes: position (Attack or Defense) and strength. Fox Ciel has m cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards X. This card mustn't be chosen before.
2. If Jiro has no alive cards at that moment, he gets the damage equal to (X's strength). Otherwise, Ciel needs to choose one Jiro's alive card Y, then:
* If Y's position is Attack, then (X's strength) β₯ (Y's strength) must hold. After this attack, card Y dies, and Jiro gets the damage equal to (X's strength) - (Y's strength).
* If Y's position is Defense, then (X's strength) > (Y's strength) must hold. After this attack, card Y dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
Input
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of cards Jiro and Ciel have.
Each of the next n lines contains a string position and an integer strength (0 β€ strength β€ 8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next m lines contains an integer strength (0 β€ strength β€ 8000) β the strength of Ciel's current card.
Output
Output an integer: the maximal damage Jiro can get.
Examples
Input
2 3
ATK 2000
DEF 1700
2500
2500
2500
Output
3000
Input
3 4
ATK 10
ATK 100
ATK 1000
1
11
101
1001
Output
992
Input
2 4
DEF 0
ATK 0
0
0
1
1
Output
1
Note
In the first test case, Ciel has 3 cards with same strength. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
Submitted Solution:
```
class KM:
def __init__(self, n, graph):
self.n = n
self.graph = graph # adjacent matrix
self.Lx = [max(self.graph[i]) for i in range(self.n)]
self.Ly = [0] * n
self.slack = None
self.S = None
self.T = None
self.matched = [-1] * n
def match(self, u):
self.S[u] = True
for v in range(self.n):
# v is already in the cross road
if self.T[v]:
continue
t = self.Lx[u] + self.Ly[v] - self.graph[u][v]
if not t:
self.T[v] = True
if self.matched[v] == -1 or self.match(self.matched[v]):
self.matched[v] = u
return True
else:
self.slack[v] = min(self.slack[v], t)
return False
def update(self):
d = min(self.slack[i] for i in range(self.n) if not self.T[i])
for i in range(self.n):
if self.S[i]:
self.Lx[i] -= d
for i in range(self.n):
if self.T[i]:
self.Ly[i] += d
else:
self.slack[i] -= d
def km(self):
for i in range(self.n):
self.slack = [0x7fffffff] * self.n
while True:
self.S = [False] * self.n
self.T = [False] * self.n
if self.match(i):
break
else:
self.update()
return sum(self.graph[self.matched[i]][i] for i in range(self.n) if self.matched[i] != -1)
# http://codeforces.com/contest/321/problem/B
if __name__ == '__main__':
inf = 0x7fffffff
maxn = 500
graph = []
for i in range(maxn):
graph.append([0] * maxn)
n, m = [int(x) for x in input().split()]
jiro = [input().split() for i in range(n)]
ciel = [int(input()) for i in range(m)]
for i in range(m):
for j in range(n):
if jiro[j][0] == 'ATK':
if ciel[i] >= int(jiro[j][1]):
graph[i][j] = ciel[i] - int(jiro[j][1])
ans = KM(m, graph).km()
if m > n:
for i in range(m):
for j in range(n, m):
graph[i][j] = ciel[i]
for i in range(m):
for j in range(n):
if jiro[j][0] == 'ATK':
if ciel[i] >= int(jiro[j][1]):
graph[i][j] = ciel[i] - int(jiro[j][1])
else:
graph[i][j] = -inf
else:
if ciel[i] > int(jiro[j][1]):
graph[i][j] = 0
else:
graph[i][j] = -inf
ans = max(ans, KM(m, graph).km())
print(ans)
``` | instruction | 0 | 97,325 | 2 | 194,650 |
No | output | 1 | 97,325 | 2 | 194,651 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,440 | 2 | 194,880 |
Tags: binary search, dp
Correct Solution:
```
n = int(input())
lis=[0]*(1000000+2)
dp=[0]*(1000000+2)
t=c=s=lef=0
for i in range(n):
a,b = map(int,input().split())
lis[a]=b
if lis[0]>0:
dp[0]=1
for i in range(1,1000000+1):
if lis[i]==0:
dp[i]=dp[i-1]
else:
if lis[i]>=i:
dp[i]=1
else:
dp[i]=dp[i-lis[i]-1]+1
print(n-max(dp))
``` | output | 1 | 97,440 | 2 | 194,881 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,441 | 2 | 194,882 |
Tags: binary search, dp
Correct Solution:
```
def bin(mas,x):
l = 0
r = len(mas)
while (r > l + 1):
m = (r + l) // 2;
if (x < mas[m]):
r = m;
else:
l = m
return l
n = int(input())
a = [-9999999]
b = [9999999]
dp = [0] * (n + 1)
for i in range(n):
x,y=[int(i) for i in input().split()]
a.append(x)
b.append(y)
a, b = (list(x) for x in zip(*sorted(zip(a, b))))
for i in range(1,n+1):
z = a[i] - b[i] - 1
x = bin(a, z)
dp[i] = dp[x] + ( i - x - 1)
ans = 10**30
for i in range(1, n + 1):
ans = min(ans, dp[i] + n - i)
print(ans)
``` | output | 1 | 97,441 | 2 | 194,883 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,442 | 2 | 194,884 |
Tags: binary search, dp
Correct Solution:
```
# your code goes here
n = int(input())
bb = [0] * 1000001
for i in range(n):
a, b = map(int, input().split())
bb[a] = b
a = 0
m = 0
for index, value in enumerate(bb):
if value > 0:
if (index - value) > 0:
a = (1 + bb[index - value -1])
else:
a = 1
bb[index] = a
print(n - max(bb))
``` | output | 1 | 97,442 | 2 | 194,885 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,443 | 2 | 194,886 |
Tags: binary search, dp
Correct Solution:
```
import bisect
from math import inf
a=[]
b=[]
maxi=10**6
ind=[-1]*(maxi+1)
n=int(input())
for _ in range(n):
x,y=map(int,input().split())
a.append([x,y])
b.append(x)
a.sort()
b.sort()
for i in range(len(b)):
ind[b[i]]=i
dp=[0]*(n)
dp[0]=0
##print(b)
for i in range(1,len(a)):
curr_pos=a[i][0]
dest=curr_pos-a[i][1]
ans=bisect.bisect_left(b,dest)-1
if ans<0:
dp[i]=ind[curr_pos]
else:
dp[i]=dp[ans]+ind[curr_pos]-ans-1
##print(dp[i],ans,curr_pos,dest)
##print(dp)
mini=inf
for i in range(len(dp)):
mini=min(mini,dp[i]+len(dp)-i-1)
print(mini)
``` | output | 1 | 97,443 | 2 | 194,887 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,444 | 2 | 194,888 |
Tags: binary search, dp
Correct Solution:
```
N = int(input())
d = [0 for i in range(1000001)]
Memo = [0 for i in range(1000001)]
max_pos = 0
for i in range(N):
subList = input().split()
index = int(subList[0])
d[index] = int(subList[1])
max_pos = max(index, max_pos)
if (d[0] != 0):
Memo[0] = 1
result = N
result = min(result, N-Memo[0])
for i in range(1, max_pos+1):
if d[i] == 0:
Memo[i] = Memo[i-1]
else:
if d[i] >= i:
Memo[i] = 1
else:
Memo[i] = Memo[i-d[i]-1]+1
result = min(result, N-Memo[i])
print(result)
``` | output | 1 | 97,444 | 2 | 194,889 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,445 | 2 | 194,890 |
Tags: binary search, dp
Correct Solution:
```
import bisect
n = int(input())
data = []
for _ in range(n):
data.append(list(map(int, input().split())))
data.sort(key=lambda x: x[0])
a = [x[0] for x in data]
b = [x[1] for x in data]
dp = [0] * n
dp[0] = 1
for i in range(1, n):
ind = bisect.bisect_left(a, a[i] - b[i], 0, i)
if ind > 0:
dp[i] = dp[ind - 1] + 1
else:
dp[i] = 1
print(n - max(dp))
``` | output | 1 | 97,445 | 2 | 194,891 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,446 | 2 | 194,892 |
Tags: binary search, dp
Correct Solution:
```
from operator import itemgetter
n = int(input())
abi = [[-10**9,0]] + [list(map(int,input().split())) for i in range(n)]
abi.sort(key = itemgetter(0))
ar = [0] * (n+1)
ar[0] = 0
def check(pos,num):
#print(pos,num)
if abi[pos][0] < num:
return True
else:
return False
def binsearch(num):
high = n+1
low = 0
mid = (high + low) // 2
while high >= low:
if check(mid,num):
low = mid + 1
else:
high = mid - 1
mid = (high + low) // 2
return mid
ans = n
for i in range(1,n+1):
num = binsearch(abi[i][0] - abi[i][1])
ar[i] = i - num - 1+ ar[num]
ans = min(ans, ar[i] + n - i)
print(ans)
``` | output | 1 | 97,446 | 2 | 194,893 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42. | instruction | 0 | 97,447 | 2 | 194,894 |
Tags: binary search, dp
Correct Solution:
```
from bisect import bisect_left as bl
n = int(input());l = []
for i in range(n):
l.append(tuple(list(map(int,input().split()))))
l.sort();DP = [0]*(n)
for i in range(n):
x = bl(l,(l[i][0]-l[i][1],0))
if x==0:
DP[i]=1
else:
DP[i]=DP[x-1]+1
print(n-max(DP))
``` | output | 1 | 97,447 | 2 | 194,895 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
import bisect
n=int(input())
ab=[list(map(int,input().split())) for i in range(n)]
ab.sort()
a=[ab[i][0] for i in range(n)]
b=[ab[i][1] for i in range(n)]
dp=[0]*n
for i in range(n):
left=a[i]-b[i]
idx=bisect.bisect_left(a,left)
if idx==0:
dp[i]=i
else:
dp[i]=dp[idx-1]+(i-idx)
ans=n
for i in range(n):
ans=min(ans,dp[i]+(n-1-i))
print(ans)
``` | instruction | 0 | 97,448 | 2 | 194,896 |
Yes | output | 1 | 97,448 | 2 | 194,897 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
n = int(input())
bb = [0] * 1000001
for _ in range(n):
a, b = map(int, input().split())
bb[a] = b
a = 0
for i, b in enumerate(bb):
if b:
if i>b :
a = (bb[i - b - 1] + 1)
else :
a=1
bb[i] = a
print(n - max(bb))
``` | instruction | 0 | 97,450 | 2 | 194,900 |
Yes | output | 1 | 97,450 | 2 | 194,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
import sys,os,io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_left , bisect_right
import math
def ii():
return int(input())
def li():
return list(map(int,input().split()))
if(os.path.exists('input.txt')):
sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w")
else:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
no = "NO"
yes = "YES"
def solve():
n = ii()
# print(n)
a = []
b = []
# print(a)
# n=len(a)
l = []
for i in range(n):
x,y = li()
l.append([x,y])
# a.append(x)
# b.append(y)
l.sort()
for i in range(n):
a.append(l[i][0])
b.append(l[i][1])
DP = [0]*(n)
for i in range(n):
x = bisect_left(a,a[i]-b[i])
if x==0:
DP[i]=1
else:
DP[i]=DP[x-1]+1
print(n-max(DP))
t = 1
# t = int(input())
for _ in range(t):
solve()
``` | instruction | 0 | 97,451 | 2 | 194,902 |
Yes | output | 1 | 97,451 | 2 | 194,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
def main():
n = int(input())
bb = [0] * 1000001
for _ in range(n):
a, b = map(int, input().split())
bb[a] = b
a = 0
for i, b in enumerate(bb):
if b:
a = (bb[i - b - 1] + 1)
bb[i] = a
print(n - max(bb))
if __name__ == '__main__':
main()
``` | instruction | 0 | 97,452 | 2 | 194,904 |
No | output | 1 | 97,452 | 2 | 194,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
n = int(input())
lis=[0]*(1000004)
dp=[0]*(1000004)
for i in range(n):
a,b = map(int,input().split())
lis[a]=b
if lis[0]>0:
dp[0]=1
for i in range(1,1000002):
if lis[i]>0:
dp[i]=max(dp[i-1],dp[max(-1,i-lis[i]-1)]+1)
else:
dp[i]=dp[i-1]
print(n-max(dp))
``` | instruction | 0 | 97,453 | 2 | 194,906 |
No | output | 1 | 97,453 | 2 | 194,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
n = int(input())
Becone = [list(map(int, input().split())) for i in range(n) ]
dp = {}
for i in range(1000001) :
dp[i] = 0
if Becone[0][0] == 0 :
dp[0] = 1
Becone.pop(0)
ans = n - dp[0]
for i in range(1, 1000001) :
if not Becone :
break
if i != Becone[0][0] :
dp[i] = dp[i-1]
continue
a,b = Becone.pop(0)
if a-b <= 0 :
dp[i] = 1
else :
dp[i] = dp[i-b-1]+1
ans = min( ans, n - dp[i] )
print( ans )
``` | instruction | 0 | 97,454 | 2 | 194,908 |
No | output | 1 | 97,454 | 2 | 194,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.
Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.
Input
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the initial number of beacons.
The i-th of next n lines contains two integers ai and bi (0 β€ ai β€ 1 000 000, 1 β€ bi β€ 1 000 000) β the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai β aj if i β j.
Output
Print a single integer β the minimum number of beacons that could be destroyed if exactly one beacon is added.
Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note
For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.
For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.
Submitted Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
def main():
d = [0] * 1000005
n = int(input())
for _ in range(n):
a, b = map(int, input().split())
d[a] = b
for i in range(1000005):
if d[i] >= i:
d[i] = 1 # sab khatam, nayi shuruat
elif d[i] > 0:
d[i] = d[i - d[i] - 1] + 1 # pichle wale ko le fata / jinda tha to add
else:
d[i] = d[i-1] # piche koi effect nahi
print(n - max(d))
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | instruction | 0 | 97,455 | 2 | 194,910 |
No | output | 1 | 97,455 | 2 | 194,911 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.
Input
The first line contains a single integer n (1 β€ n β€ 18) β the number of participants of the Sith Tournament.
Each of the next n lines contains n real numbers, which form a matrix pij (0 β€ pij β€ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.
The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants.
Output
Output a real number β the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.
Examples
Input
3
0.0 0.5 0.8
0.5 0.0 0.4
0.2 0.6 0.0
Output
0.680000000000000 | instruction | 0 | 97,473 | 2 | 194,946 |
Tags: bitmasks, dp, math, probabilities
Correct Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
def main():
n = int(input())
p = [list(map(float,input().split())) for _ in range(n)]
y = 1<<n
dp = [[0]*y for _ in range(n)]
# dp[i][j] is prob of 0 win when j fought and i left
dp[0][y-1] = 1
for i in range(y-2,-1,-1):
mask = 1
for j in range(n):
if not mask&i:
mask <<= 1
continue
mask1 = 1
for k in range(n):
if i&mask1:
mask1 <<= 1
continue
dp[j][i] = max(dp[j][i],
dp[j][i|mask1]*p[j][k]+dp[k][i|mask1]*p[k][j])
mask1 <<= 1
mask <<= 1
print(max(dp[i][1<<i] for i in range(n)))
# Fast IO Region
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main()
``` | output | 1 | 97,473 | 2 | 194,947 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The rules of Sith Tournament are well known to everyone. n Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory.
Input
The first line contains a single integer n (1 β€ n β€ 18) β the number of participants of the Sith Tournament.
Each of the next n lines contains n real numbers, which form a matrix pij (0 β€ pij β€ 1). Each its element pij is the probability that the i-th participant defeats the j-th in a duel.
The elements on the main diagonal pii are equal to zero. For all different i, j the equality pij + pji = 1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants.
Output
Output a real number β the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10 - 6.
Examples
Input
3
0.0 0.5 0.8
0.5 0.0 0.4
0.2 0.6 0.0
Output
0.680000000000000
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
def func(step, w, l, p, n, P):
if step == n-1:
return P*p[0][w[step-1]]
ans = 0
for i in range(1, n):
if (i in w) or (i in l):
continue
w[step] = w[step-1]
l[step] = i
a = func(step+1, w, l, p, n, P*p[w[step]][l[step]]);
w[step] = i
l[step] = w[step-1]
a = a + func(step+1, w, l, p, n, P*p[w[step]][l[step]]);
if a > ans:
ans = a
w[step] = - 1
l[step] = - 1
return ans
n = int(input())
p = [ [float(i) for i in input().split()] for j in range(n) ]
start = time.time()
w = [ -1 for i in range(n) ]
l = [ -1 for i in range(n) ]
ans = 0
for i in range(1, n):
w [0] = i
ans = func(1, w, l, p, n, 1)
print(ans)
finish = time.time()
#print(finish - start)
``` | instruction | 0 | 97,476 | 2 | 194,952 |
No | output | 1 | 97,476 | 2 | 194,953 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.