ZhenlongDai/ACPR huggingface dataset:
Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from sys import exit
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
ul = 1
dl = H
rl = W
ll = 1
for s, t in zip(S[1::][::-1], T[::-1]):
    if t == 'U':
        dl = min(H, dl+1)
    elif t == 'D':
        ul = max(1, ul-1)
    elif t == 'L':
        rl = min(W, rl+1)
    else:
        ll = max(1, ll-1)
    if s == 'U':
        ul += 1
    elif s == 'D':
        dl -= 1
    elif s == 'L':
        ll += 1
    else:
        rl -= 1
    if ul > dl or ll > rl or ul == H + 1 or dl == 0 or ll == W + 1 or rl == 0:
        print('NO')
        exit()
s = S[0]
if s == 'U':
    ul += 1
elif s == 'D':
    dl -= 1
elif s == 'L':
    ll += 1
else:
    rl -= 1

if ul <= sr <= dl and ll <= sc <= rl:
    print('YES')
else:
    print('NO')

Failing test case input:
2 3 3
2 2
RLR
LUD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
from sys import exit
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
ul = 1
dl = H
rl = W
ll = 1
for s, t in zip(S[1::][::-1], T[:-1:][::-1]):
    if s == 'U':
        ul += 1
    elif s == 'D':
        dl -= 1
    elif s == 'L':
        ll += 1
    else:
        rl -= 1
    if ul > dl or ll > rl or ul == H + 1 or dl == 0 or ll == W + 1 or rl == 0:
        print('NO')
        exit()
    if t == 'U':
        dl = min(H, dl+1)
    elif t == 'D':
        ul = max(1, ul-1)
    elif t == 'L':
        rl = min(W, rl+1)
    else:
        ll = max(1, ll-1)
s = S[0]
if s == 'U':
    ul += 1
elif s == 'D':
    dl -= 1
elif s == 'L':
    ll += 1
else:
    rl -= 1

if ul <= sr <= dl and ll <= sc <= rl:
    print('YES')
else:
    print('NO')
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()


x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()

x=R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")

Failing test case input:
4 10 2
2 1
RLDRRUDDLSL
URRDRLLDLRD

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()


x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if l==0:
        l=1
    if r>w:
        r=w
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()

x=h+1-R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if l==0:
        l=1
    if r>h:
        r=h
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")
Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
h -= 1
w -= 1
s = input()
t = input()
l,r,d,u = 0,w,h,0
for i in range(n)[::-1]:
    if t[i] == 'R':
        if l > 0:
            l -= 1
    elif t[i] == 'L':
        if r < w:
            r += 1
    elif t[i] == 'U':
        if u < h:
            d -= 1
    elif t[i] == 'D':
        if d > 0:
            u += 1
    if s[i] == 'R':
        r -= 1
        if r < 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        l += 1
        if l > w:
            print('NO')
            quit()
    elif s[i] == 'U':
        u += 1
        if u > h:
            print('NO')
            quit()
    elif s[i] == 'D':
        d -= 1
        if d < 0:
            print('NO')
            quit()
    
if l <= sc <= r and u <= sr <= d:
    print('YES')
else:
    print('NO')
            

Failing test case input:
4 4 5
2 2
UDRRR
LLDTD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w,n = LI()
sr,sc = LI()
sr -= 1
sc -= 1
h -= 1
w -= 1
s = input()
t = input()
l,r,d,u = 0,w,h,0
for i in range(n)[::-1]:
    if t[i] == 'R':
        if l > 0:
            l -= 1
    elif t[i] == 'L':
        if r < w:
            r += 1
    elif t[i] == 'U':
        if d < h:
            d += 1
    elif t[i] == 'D':
        if u > 0:
            u -= 1
    if s[i] == 'R':
        r -= 1
        if r < 0:
            print('NO')
            quit()
    elif s[i] == 'L':
        l += 1
        if l > w:
            print('NO')
            quit()
    elif s[i] == 'U':
        u += 1
        if u > h:
            print('NO')
            quit()
    elif s[i] == 'D':
        d -= 1
        if d < 0:
            print('NO')
            quit()
    
if l <= sc <= r and u <= sr <= d:
    print('YES')
else:
    print('NO')
Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

h, w,n = map(int, input().split())
r,c = map(int, input().split())
s = input() 
t = input() 

toL = c
toR = w-c+1
toU = r
toD = h-r+1


cntR=0
for i in range(n):
    if s[i] == 'R' : cntR+=1
    if cntR == toR:
        print('NO')
        exit()
    if t[i] == 'L' and cntR>0 : cntR-=1


cntL = 0
for i in range(n):
    if s[i] == 'L' : cntL+=1
    if cntL == toL:
        print('NO')
        exit()
    if t[i] == 'R' and cntL>0: cntL-=1


cntU = 0
for i in range(n):
    if s[i] == 'U' : cntU+=1
    if cntU == toU:
        print('NO')
        exit()
    if t[i] == 'D' and cntU>0: cntU-=1


cntD = 0
for i in range(n):
    if s[i] == 'D' : cntD+=1
    if cntD == toD:
        print('NO')
        exit()
    if t[i] == 'U' and cntD>0: cntD-=1

print('YES')


Failing test case input:
4 0 5
-1 2
UDRQR
LLDTD

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

h, w,n = map(int, input().split())
r,c = map(int, input().split())
s = input() 
t = input() 


x=c
for i in range(n):
    if s[i] == 'R' : x+=1
    if x == w+1:
        print('NO')
        exit()
    if t[i] == 'L' and x>1 : x-=1


x=c
for i in range(n):
    if s[i] == 'L' : x-=1
    if x == 0:
        print('NO')
        exit()
    if t[i] == 'R' and x<w: x+=1


y=r
for i in range(n):
    if s[i] == 'U' : y-=1
    if y == 0:
        print('NO')
        exit()
    if t[i] == 'D' and y<h: y+=1


y=r
for i in range(n):
    if s[i] == 'D' : y+=1
    if y == h+1:
        print('NO')
        exit()
    if t[i] == 'U' and y>1: y-=1

print('YES')
Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
h,w,n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()

left = 1
right = w
bot = 1
top = h


if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    top -= 1
elif s[-1] == "D":
    bot += 1

for i in range(n-2, -1, -1):
    
    
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(0, left-1)
    elif t[i] == "U":
        bot = max(0, bot-1)
    elif t[i] == "D":
        top = min(h, top+1)
    
    
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        top -= 1
    elif s[i] == "D":
        bot += 1
    if left >= right or bot >= top:
        print("NO")
        exit()


if left <= sc <= right and bot <= sr <=top:
    print("YES")
else:
    print("NO")

Failing test case input:
2 3 3
2 2
RLR
LUD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
h,w,n = map(int, input().split())
sr, sc = map(int, input().split())
s = input()
t = input()

left = 1
right = w
bot = 1
top = h

if s[-1] == "L":
    left += 1
elif s[-1] == "R":
    right -= 1
elif s[-1] == "U":
    bot += 1
elif s[-1] == "D":
    top -= 1

for i in range(n-2, -1, -1):
    if t[i] == "L":
        right = min(w, right+1)
    elif t[i] == "R":
        left = max(1, left-1)
    elif t[i] == "U":
        top = min(h, top+1)
    elif t[i] == "D":
        bot = max(1, bot-1)
    if s[i] == "L":
        left += 1
    elif s[i] == "R":
        right -= 1
    elif s[i] == "U":
        bot += 1
    elif s[i] == "D":
        top -= 1
    if left > right or bot > top:
        print("NO")
        exit()
if left <= sc <= right and bot <= sr <=top:
    print("YES")
else:
    print("NO")
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
h,w,n=[int(j) for j in input().split()]
r,c=[int(j) for j in input().split()]
s=input()
t=input()


x=c
for i in range(n):
    if s[i]=="R":
        x+=1
    if t[i]=="L" and x>1:
        x-=1
if not 1<=x<=w:
    print("NO")
    exit()


x=c
for i in range(n):
    if s[i]=="L":
        x-=1
    if t[i]=="R" and x<w:
        x+=1
if not 1<=x<=w:
    print("NO")
    exit()


x=r
for i in range(n):
    if s[i]=="U":
        x+=1
    if t[i]=="D" and x>1:
        x-=1
if not 1<=x<=h:
    print("NO")
    exit()


x=r
for i in range(n):
    if s[i]=="D":
        x-=1
    if t[i]=="U" and x<h:
        x+=1
if not 1<=x<=h:
    print("NO")
    exit()
print("YES")

Failing test case input:
4 10 2
2 1
RLDRRUDDLSL
URRDRLLDLRD

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()


x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if l==0:
        l=1
    if r>w:
        r=w
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()

x=h+1-R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if l==0:
        l=1
    if r>h:
        r=h
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")
Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
A = III()
B = III()


dp = [0]*(N+1)
for i in range(N+1):
    for j in range(3):
        if i-A[j]>=0:
            dp[i] = max(dp[i-A[j]]+B[j],dp[i])
for i in range(N+1):
    dp[i] = N-i+dp[i]


count = max(dp)
dp = [0]*(count+1)
for i in range(count+1):
    for j in range(3):
        if i-B[j]>=0:
            dp[i] = max(dp[i-B[j]]+A[j],dp[i])
for i in range(N+1):
    dp[i] = count-i+dp[i]

print(max(dp))

Failing test case input:
23
1 2 1
2 0 1

Expected output:
48


Actual output:
71


Fixed submission (verdict: Accepted):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



def solve():
    N = I()
    A = III()
    B = III()

    first_use = []
    second_use = []
    for i in range(3):
        if A[i]<B[i]:
            first_use.append(i)
        if A[i]>B[i]:
            second_use.append(i)

    def change(n,A,B,use_index):
        dp = [n]*(n+1)
        for i in range(n+1):
            for j in use_index:
                if i-A[j]>=0:
                    dp[i] = max(dp[i-A[j]]+B[j]-A[j],dp[i])
        return dp[n]

    count1 = change(N,A,B,first_use)
    count2 = change(count1,B,A,second_use)

    print(count2)

solve()
Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        j = M 
        k = (M - X[1] * j) 
        res = max(res, dg * i + ds * j + db * k)
        k = M 
        j = (M - X[2] * k) 
        res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)



Failing test case input:
23
2 2 1
5 0 2

Expected output:
59


Actual output:
228


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
INF = 10**18
A = [INF] * 3
B = [INF] * 3
values = [abs(ga - gb), abs(sa - sb), abs(ba - bb)]
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    dp = [0] * (N+1)
    dp[0] = 0
    for x, v in zip(X, values):
        for i in range(N+1-x):
            dp[i+x] = max(dp[i+x], dp[i] + v)
    return dp[-1]

N += calc(A)
N += calc(B)
print(N)

Problem: p03008
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

n = int(input())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
def f(n, a1, a2, a3, b1, b2, b3):
  dp = [0]*(n+1)
  for i in range(n):
    for a, b in zip((a1, a2, a3), (b1, b2, b3)):
      if i+a <= n:
        dp[i+a] = max(dp[i+a], dp[i]+b)
  return max(dp)
m = f(n, a1, a2, a3, b1, b2, b3)
ans = f(m, b1, b2, b3, a1, a2, a3)
print(ans)

Failing test case input:
23
3 3 2
5 0 2

Expected output:
40


Actual output:
93


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = list(range(n+1))
  for a, b in zip(A, B):
    for i in range(n+1):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp)
m = f(n)
A, B = B, A
ans = f(m)
print(ans)
Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
A = III()
B = III()

first_use = []
second_use = []
for i in range(3):
    if A[i]<B[i]:
        first_use.append(i)
    if A[i]>B[i]:
        second_use.append(i)


dp = [0]*(N+1)
for i in range(N+1):
    for j in first_use:
        if i-A[j]>=0:
            dp[i] = max(dp[i-A[j]]+B[j],dp[i])
for i in range(N+1):
    dp[i] = N-i+dp[i]


count = max(dp)
dp = [0]*(count+1)
for i in range(count+1):
    for j in second_use:
        if i-B[j]>=0:
            dp[i] = max(dp[i-B[j]]+A[j],dp[i])
for i in range(N+1):
    dp[i] = count-i+dp[i]

print(max(dp))

Failing test case input:
23
0 5 2
2 2 2

Expected output:
61


Actual output:
60


Fixed submission (verdict: Accepted):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



def solve():
    N = I()
    A = III()
    B = III()

    first_use = []
    second_use = []
    for i in range(3):
        if A[i]<B[i]:
            first_use.append(i)
        if A[i]>B[i]:
            second_use.append(i)

    def change(n,A,B,use_index):
        dp = [n]*(n+1)
        for i in range(n+1):
            for j in use_index:
                if i-A[j]>=0:
                    dp[i] = max(dp[i-A[j]]+B[j]-A[j],dp[i])
        return dp[n]

    count1 = change(N,A,B,first_use)
    count2 = change(count1,B,A,second_use)

    print(count2)

solve()
Problem: p03008
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

n = int(input())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
def f(n, a1, a2, a3, b1, b2, b3):
  dp = [0]*(n+1)
  for i in range(n+1):
    for a, b in zip((a1, a2, a3), (b1, b2, b3)):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp)
m = f(n, a1, a2, a3, b1, b2, b3)
ans = f(m, b1, b2, b3, a1, a2, a3)
print(ans)

Failing test case input:
23
2 2 1
3 0 1

Expected output:
36


Actual output:
47


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = list(range(n+1))
  for a, b in zip(A, B):
    for i in range(n+1):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp)
m = f(n)
A, B = B, A
ans = f(m)
print(ans)

Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for v,w in zip(values,weights):
        for c in range(w, capacity+1):
            work[c]=max(work[c],work[c-w]+v)
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    print(valA)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())

Failing test case input:
23
0 8 1
2 1 1

Expected output:
200


Actual output:
25
200


Fixed submission (verdict: Accepted):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for v,w in zip(values,weights):
        for c in range(w, capacity+1):
            work[c]=max(work[c],work[c-w]+v)
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())
Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
g1,s1,b1 = map(int,input().split())
g2,s2,b2 = map(int,input().split())
def trade(n,g1,s1,b1,g2,s2,b2):
  tradelist = []
  for x,y in (g1,g2),(s1,s2),(b1,b2):
    if x < y:
      tradelist.append((x,y))
  if not tradelist:
    return n
  dp = [0]*(n+1)
  for w,v in tradelist:
    for i in range(n+1-w):
      dp[i+w] = max(dp[i+w],dp[i]+v)
  return dp[n]

ans1 = trade(n,g1,s1,b1,g2,s2,b2)
ans2 = trade(ans1,g2,s2,b2,g1,s1,b1)
print(ans2)

Failing test case input:
23
2 2 1
5 0 2

Expected output:
59


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
g1,s1,b1 = map(int,input().split())
g2,s2,b2 = map(int,input().split())
def trade(n,g1,s1,b1,g2,s2,b2):
  tradelist = []
  for x,y in (g1,g2),(s1,s2),(b1,b2):
    if x < y:
      tradelist.append((x,y))
  if not tradelist:
    return n
  dp = [0]*(n+1)
  for w,v in tradelist:
    for i in range(n+1-w):
      dp[i+w] = max(dp[i+w],dp[i]+v)
  ptr = n
  ret = dp[n]
  while ptr and dp[ptr-1] == dp[n]:
    ret += 1
    ptr -= 1
  return ret

ans1 = trade(n,g1,s1,b1,g2,s2,b2)
ans2 = trade(ans1,g2,s2,b2,g1,s1,b1)
print(ans2)

Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for c in range(1, capacity+1):
        candidates = [val + work[c-weight]
                      for val,weight in zip(values,weights) if weight <= c]
        work[c] = max(candidates) if candidates != [] else 0
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))
    prices_b=list(map(int,inp().split()))
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())

Failing test case input:
23
0 2 1
4 0 1

Expected output:
29


Actual output:
27


Fixed submission (verdict: Accepted):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for v,w in zip(values,weights):
        for c in range(w, capacity+1):
            work[c]=max(work[c],work[c-w]+v)
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())
Problem: p03008
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for c in range(1, capacity+1):
        candidates = [val + work[c-weight]
                      for val,weight in zip(values,weights) if weight <= c]
        work[c] = max(candidates) if candidates != [] else 0
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)

Failing test case input:
23
0 3 1
2 2 1

Expected output:
37


Actual output:

Fixed submission (verdict: Accepted):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for v,w in zip(values,weights):
        for c in range(w, capacity+1):
            work[c]=max(work[c],work[c-w]+v)
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())
Problem: p03752
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n, k = map(int, input().split())
li = list(map(int, input().split()))
min_ = 10**12
for i in range(1<<n):
    max_height = li[0]
    money = 0
    can_look = 0
    li_h = []
    for j in range(n):
        if j != 0:
            max_height = max(max_height, li_h[j-1])
        
        if j == 0:
            can_look += 1
            li_h.append(li[0])
            continue
        if (i>>j)&1:
            can_look += 1
            if li[j] <= max_height:
                li_h.append(max_height+1)
            else:
                li_h.append(li[j])
        else:
            li_h.append(li[j])
    if can_look >= k:
        money = sum(li_h) - sum(li)
        min_ = min(min_, money)
        print(li_h)
        print(li)
        
print(min_)

Failing test case input:
5 4
484 3774 3598 1733 2940

Expected output:
1013


Actual output:
[484, 3774, 3775, 3776, 2940]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 3776, 2940]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 1733, 3776]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 1733, 3776]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3598, 3775, 3776]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3598, 3775, 3776]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 3776, 3777]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 3776, 3777]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 3776, 3777]
[484, 3774, 3598, 1733, 2940]
[484, 3774, 3775, 3776, 3777]
[484, 3774, 3598, 1733, 2940]
1013


Fixed submission (verdict: Accepted):
n, k = map(int, input().split())
li = list(map(int, input().split()))
min_ = 10**12
for i in range(1<<n):
    max_height = li[0]
    money = 0
    can_look = 0
    li_h = []
    for j in range(n):
        if j != 0:
            max_height = max(max_height, li_h[j-1])
        
        if j == 0:
            can_look += 1
            li_h.append(li[0])
            continue
        if (i>>j)&1:
            can_look += 1
            if li[j] <= max_height:
                li_h.append(max_height+1)
            else:
                li_h.append(li[j])
        else:
            li_h.append(li[j])
    if can_look >= k:
        money = sum(li_h) - sum(li)
        min_ = min(min_, money)

print(min_)
Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys, itertools
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    a = LI()
    ans = INF
    
    for i in itertools.combinations(range(1, N), K-1):
        cost_sum = 0
        for j, e in enumerate(i):
            cost_sum += (a[0]+j+1)-a[e]
        ans = min(cost_sum, ans)

    print(ans)

if __name__ == '__main__':
    resolve()

Failing test case input:
5 5
2541 3649 1332 110 2597

Expected output:
6914


Actual output:
2486


Fixed submission (verdict: Accepted):
import sys, itertools
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    N, K = LI()
    a = LI()
    ans = INF
    for i in range(2**N):
        cost_sum = 0
        cnt = 0
        prev_height = 0
        for j in range(N):
            if (i>>j)&1:
                if a[j]<=prev_height:
                    cost_sum += (prev_height+1)-a[j]
                    prev_height += 1
                else:
                    prev_height = a[j]
                cnt += 1
            prev_height = max(a[j], prev_height)
        if cnt>=K:
            ans = min(cost_sum, ans)

    print(ans)

if __name__ == '__main__':
    resolve()
Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N,K=list(map(int,input().split()))
height=list(map(int,input().split()))
ans=10**10
count=0
for x in range(1, N):
    if height[0] < height[x]:
        count += 1
if count>=K:
    print(0)
    exit()
for i in range(2**N):
    cost=0
    height2=height.copy()
    for j in range(N):
        if i>>j&1:
            count=0
            for k in range(j):
                if height2[k]>=height2[j]:
                    cost+=height2[k]-height2[j]+1
                    height2[j]+=height2[k]-height2[j]+1
            for x in range(1,N):
                if height2[0]<height2[x]:
                    count+=1

            if ans>cost and count==K-1:
                ans=cost


print(ans)

Failing test case input:
5 5
862 3170 5890 212 3424

Expected output:
8147


Actual output:
5679


Fixed submission (verdict: Accepted):
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
ans = 10 ** 10
if N==K:
    cost=0
    for i in range(1,N):
        if height[i-1]>=height[i]:
            cost+=height[i-1]-height[i]+1
            height[i]+=height[i-1]-height[i]+1
    print(cost)
    exit()
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    for j in range(N):
        if i >> j & 1:
            if j!=0:
                tarou=max(height2[:j])
                if height2[j]<=tarou:
                    cost+=tarou-height2[j]+1
                    height2[j]+=tarou-height2[j]+1
                    count+=1
                else:count+=1
    
    
    
    
    
    if ans > cost and count >= K:
        ans = cost

print(ans)
Problem: p03752
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 10**11


for i in range(2**(n-1)):
  select = [0]
  len_select = 1
  for j in range(n-1):
    if i >> j & 1 and len_select < k:
      select.append(j+1)
      len_select += 1
  
  
  ok = 1
  
  for m in select:
    for l in range(n):
      if l in select: continue
      if l < k and a[l] >= a[m]:
        
        ok = 0
  if ok == 0 or len_select < k: continue
  else:
    ans_i = 0
    pre_val = a[0]
    for j in range(1,k):
      
      if pre_val >= a[select[j]]:
        ans_i += pre_val+1 - a[select[j]]
        pre_val += 1
      else:
        pre_val = a[select[j]]
  ans = min(ans,ans_i)
print(ans)

Failing test case input:
5 4
484 3774 3598 1733 2940

Expected output:
1013


Actual output:
2220


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
a = list(map(int,input().split()))
ans = 10**11


for i in range(2**(n-1)):
  select = [0]
  len_select = 1
  for j in range(n-1):
    if i >> j & 1 and len_select < k:
      select.append(j+1)
      len_select += 1
  
  
  ok = 1
  
  for m in select:
    for l in range(n):
      if l in select: continue
      if l < m and a[l] >= a[m]:
        
        ok = 0
  if ok == 0 or len_select < k: continue
  else:
    ans_i = 0
    pre_val = a[0]
    for j in range(1,k):
      
      if pre_val >= a[select[j]]:
        ans_i += pre_val+1 - a[select[j]]
        pre_val += 1
      else:
        pre_val = a[select[j]]
  ans = min(ans,ans_i)
print(ans)
Problem: p03752
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import itertools
import copy

def main():
    N,K = map(int, input().split())
    a_list = [int(e) for e in input().split()]

    
    
    
    

    ans = 10**100

    l = [i for i in range(0,N)]

    for v in itertools.combinations(l,K): 
        copied_a_list = copy.copy(a_list)
        
        v = set(v)
        left_max = copied_a_list[0]-1
        temp_ans = 0
        
        for i in range(N): 
            if i in v:
                temp_ans += max(0,left_max+1 - copied_a_list[i])
                copied_a_list[i] = left_max+1
            
            if copied_a_list[i] > left_max:
                left_max = copied_a_list[i]
        
        print(v,copied_a_list,temp_ans)
        
        if temp_ans < ans:
            ans = temp_ans
            
    print(ans)

main()

Failing test case input:
5 5
1128 875 11055 3469 5003

Expected output:
13895


Actual output:
{0, 1, 2, 3, 4} [1128, 1129, 1130, 1131, 1132] 254
254


Fixed submission (verdict: Accepted):
import itertools
import copy

def main():
    N,K = map(int, input().split())
    a_list = [int(e) for e in input().split()]

    
    
    
    

    ans = 10**100

    l = [i for i in range(0,N)]

    for v in itertools.combinations(l,K): 
        copied_a_list = copy.copy(a_list)
        
        v = set(v)
        left_max = copied_a_list[0]-1
        temp_ans = 0
        
        for i in range(N): 
            if i in v:
                temp_ans += max(0,left_max+1 - copied_a_list[i])
                copied_a_list[i] = max(left_max+1,copied_a_list[i])
            
            if copied_a_list[i] > left_max:
                left_max = copied_a_list[i]
        
        
        
        if temp_ans < ans:
            ans = temp_ans
            
    print(ans)

main()

Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k = list(map(int, input().split()))
a=list(map(int, input().split()))

ans=[]
for i in range(1<<n):
    
    if bin(i).count("1")==k:
        cst=0
        use=a.copy()
        for u in range(1,n):
            if(i>>u&1):
                left_max=max(use[:u])
                cst+=left_max-use[u]+1
                use[u]= left_max+1
        ans.append(cst)   
print(min(ans))


Failing test case input:
5 5
1993 875 9355 3469 5003

Expected output:
11360


Actual output:
-10720


Fixed submission (verdict: Accepted):
n,k = list(map(int, input().split()))
a=list(map(int, input().split()))

ans=[]
for i in range(1<<n):
    
    if bin(i).count("1")==k:
        cst=0
        use=a.copy()
        for u in range(1,n):
            if(i>>u&1):
                left_max=max(use[:u])
                cst+=left_max-use[u]+1 if left_max>=use[u] else 0
                use[u]= max(left_max+1, use[u])
        ans.append(cst)
print(min(ans))
Problem: p03752
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = sum(A)
for key in range(1 << N):
    if bin(key).count('1') < K:
        continue
    cost = 0
    height = 0
    for i in range(N):
        if not (key & (1 << i)):
            height = max(A[i], height)
            continue
        height = max(A[i], height+1)
        cost += height - A[i]
    ans = min(ans, cost)
print(ans)






Failing test case input:
5 5
1993 875 26226 3469 5003

Expected output:
45102


Actual output:
37566


Fixed submission (verdict: Accepted):
N, K = map(int, input().split())
A = list(map(int, input().split()))

ans = float('inf')
for key in range(1 << N):
    if bin(key).count('1') < K:
        continue
    cost = 0
    height = 0
    for i in range(N):
        if not (key & (1 << i)):
            height = max(A[i], height)
            continue
        height = max(A[i], height+1)
        cost += height - A[i]
    ans = min(ans, cost)
print(ans)

Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
_min = 1145141919810
for i in range(2**(n-1)):
    ctr = 0
    val = 0
    flag = 0
    h_max = a[0]
    for j in range(n-1):
        if (i>>j)&1:
            ctr+=1
            h_max = h_max+1 if h_max>=a[j+1] else a[j+1]
            val+=h_max-a[j+1]
            if ctr>=k-1:
                flag=1
                break
    if val<_min and flag and h_max<=10**9:
        _min = val
print(_min)

Failing test case input:
5 1
5960 3170 3598 610 3424

Expected output:
0


Actual output:
2363


Fixed submission (verdict: Accepted):
n,k = map(int,input().split())
a = [int(x) for x in input().split()]
_min = 1145141919810
for i in range(2**(n-1)):
    ctr = 0
    val = 0
    h_max = a[0]
    for j in range(n-1):
        if (i>>j)&1:
            ctr+=1
            h_max = h_max+1 if h_max>=a[j+1] else a[j+1]
            val+=h_max-a[j+1]
        if h_max<a[j+1]:
            h_max = a[j+1]
    if ctr==k-1 and val<_min:
        _min = val
print(_min)
Problem: p03752
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N, K = [int(i)for i in input().split()]
A = [int(i) for i in input().split()]


ans = float("inf")
for b in range(2 ** N):
    cnt = 0
    cost = 0
    height = A[0]
    for i in range(N):
        if i == 0:
            continue
        if (b >> i) & 1:
            cnt += 1
            cost += height - A[i] + 1
            height += 1
    if cnt + 1 == K:
        ans = min(ans, cost)

print(ans)


Failing test case input:
5 5
1993 607 9355 3469 4674

Expected output:
11957


Actual output:
-10123


Fixed submission (verdict: Accepted):
N, K = [int(i)for i in input().split()]
A = [int(i) for i in input().split()]


ans = float("inf")
for b in range(2 ** (N - 1)):
    cnt = 0
    cost = 0
    height = A[0]
    for i in range(N - 1):
        if height - A[i + 1] < 0:
            cnt += 1
            height = A[i + 1]
        elif (b >> i) & 1:
            cnt += 1
            cost += height - A[i + 1] + 1
            height += 1
    if cnt + 1 >= K:
        ans = min(ans, cost)

print(ans)

Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1!=bit.count(1):
        continue
    NO_bit1 = [k for k in range(N-1) if bit[k]==1]
    temp = 0
    for l,x in enumerate(NO_bit1,1):
        if a[x+1]>=a[0]+l:
            continue
        temp += (a[0]+l)-a[x+1]
    ans = min(ans,temp)
print(ans)


Failing test case input:
5 5
862 3170 5890 212 3424

Expected output:
8147


Actual output:
653


Fixed submission (verdict: Accepted):
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1!=bit.count(1):
        continue
    temp,kijun = 0,a[0]
    for k in range(N-1):
        if bit[k]==0:
            kijun = max(kijun,a[k+1])
        else:
            if a[k+1]>=kijun+1:
                kijun = a[k+1]
                continue
            temp += (kijun+1)-a[k+1]
            kijun += 1
    ans = min(ans,temp)
print(ans)
Problem: p03752
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n, k = [int(i) for i in input().split()]
aa = [int(i) for i in input().split()]

ans = float("inf")
for bit in range(1<<n):
    if bin(bit).count("1") < k:
        continue
    if not (bit>>(n-1) & 1):
        continue
    now = aa[0]
    cost = 0
    for i in range(1, n):
        if bit>>i & 1:
            if aa[i] > now:
                now = aa[i]
            else:
                now = now + 1
                cost += now - aa[i]
        else:
            if aa[i] > now:
                continue

    ans = min(ans, cost)

print(ans)


Failing test case input:
5 4
11683 3774 1788 4173 952

Expected output:
25320


Actual output:
26156


Fixed submission (verdict: Accepted):
import itertools

n, k = [int(i) for i in input().split()]
top, *a = [int(i) for i in input().split()]

ans = float("inf")
for pp in itertools.product((0, 1), repeat=n-1):
    now = top
    v = 1
    cost = 0
    for i, p in enumerate(pp):
        if p == 0:
            if a[i] > now:
                now = a[i]
                v += 1
            continue
        now = max(now+1, a[i])
        cost += max(now-a[i], 0)
        v += 1
    if v >= k:
        ans = min(ans, cost)
print(ans)

Problem: p03752
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from itertools import combinations

N, K = map(int, input().split())
A = list(map(int, input().split()))

best_score = float("inf")
for sel in combinations(range(N), K):
    cost = 0

    
    
    first = sel[0]
    first_max = A[first]
    for k in range(first):
        first_max = max(first_max, A[k])
    current = first_max
    if first_max > 0:
        cost += abs(A[first] - current) 
    
    for i in range(1, K):
        if current > A[sel[i]]:
            cost += abs(A[sel[i]] - current) + 1
            current += 1
        else:
            current = A[sel[i]] + 1
    
    best_score = min(best_score, cost)
print(best_score)

Failing test case input:
5 5
2312 3208 19389 3469 3424

Expected output:
31888


Actual output:
31890


Fixed submission (verdict: Accepted):
from itertools import combinations

N, K = map(int, input().split())
A = list(map(int, input().split()))














    










best_score = float("inf")
for sel in combinations(range(N), K):
    sel = sorted(sel)
    cost = 0
    m = -1

    for i in range(N):
        if i not in sel:
            
            m = max(m, A[i])
        else:
            if m >= A[i]:
                cost += 1 + m - A[i]
                m += 1
            else:
                m = A[i]
    best_score = min(best_score, cost)
print(best_score)
Problem: p03752
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from itertools import combinations

def getval():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    return n,k,a

def main(n,k,a):
    arr = []
    locmax = 0
    for i in a:
        arr.append(locmax)
        locmax = max(locmax,i)

    ans = 2**60
    for c in combinations(range(1,n),k-1):
        cost = 0
        prev = a[0]
        for i in c:
            if a[i]<=prev:
                cost += max(prev,arr[i])+1-a[i]
            prev = max(prev,arr[i])+1
        ans = min(ans,cost)
    print(ans)

if __name__=="__main__":
    n,k,a = getval()
    main(n,k,a) 

Failing test case input:
5 5
862 2044 1332 56 2597

Expected output:
2703


Actual output:
1990


Fixed submission (verdict: Accepted):
from itertools import combinations

def getval():
    n,k = map(int,input().split())
    a = list(map(int,input().split()))
    return n,k,a

def main(n,k,a):
    arr = []
    locmax = 0
    for i in a:
        arr.append(locmax)
        locmax = max(locmax,i)

    ans = 2**60
    for c in combinations(range(1,n),k-1):
        cost = 0
        prev = a[0]
        for i in c:
            if a[i]<=prev:
                cost += max(prev,arr[i])+1-a[i]
            prev = max(max(prev,arr[i])+1,a[i])
        ans = min(ans,cost)
    if k==1:
        ans = 0
    print(ans)

if __name__=="__main__":
    n,k,a = getval()
    main(n,k,a)

Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def dfs(index):
    global ans
    if index == N:
        if blst.count(1) == K - 1:
            tmp = a[0]
            anstmp = 0
            for i in range(1, N):
                if blst[i - 1] == 0:continue
                if tmp >= a[i]:
                    anstmp += tmp - a[i] + 1
                    tmp += 1
                else:
                    tmp = a[i]
            ans = min(ans, anstmp)
        return
    for i in range(2):
        blst[index - 1] = i
        dfs(index + 1)
    return

N, K = map(int, input().split())
a = list(map(int, input().split()))
blst = [0] * (N - 1)
ans = float('inf')
dfs(1)

print(0)

Failing test case input:
5 5
862 3170 6694 212 3424

Expected output:
9755


Actual output:
0


Fixed submission (verdict: Accepted):
def dfs(index):
    global ans
    if index == N:
        if blst.count(1) >= K - 1:
            tmp = a[0]
            anstmp = 0
            for i in range(1, N):
                if blst[i - 1] == 0:
                    if tmp < a[i]:
                        tmp = a[i]
                    continue
                if tmp >= a[i]:
                    anstmp += tmp - a[i] + 1
                    tmp += 1
                else:
                    tmp = a[i]
            ans = min(ans, anstmp)
        return
    for i in range(2):
        blst[index - 1] = i
        dfs(index + 1)
    return

N, K = map(int, input().split())
a = list(map(int, input().split()))
blst = [0] * (N - 1)
ans = float('inf')
dfs(1)

print(ans)
Problem: p03752
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import copy

N,K = map(int,input().split())
A = list(map(int,input().split()))

ans = [10 ** 12 for i in range(2 ** (N - 1))]
seen = 1  

for i in range(2 ** (N - 1)):
  AA = copy.deepcopy(A)
  cost = 0
  con = 0
  for j in range(N - 1):
    m = max(AA[:j + 1])
    a = AA[j + 1]
    if ((i >> j) & 1) and a <= m:
      cost += (m - a + 1)
      AA[j + 1] = m + 1
      con += 1
    if con == K - 1:
      break
  
  if len([AA[i] for i in range(1,N) if AA[i] > A[0]]) >= K - 1:
    ans[i] = cost
  
print(min(ans))

Failing test case input:
5 5
862 3408 1332 212 3035

Expected output:
5651


Actual output:
3197


Fixed submission (verdict: Accepted):
import copy

N,K = map(int,input().split())
A = list(map(int,input().split()))

ans = [10 ** 12 for i in range(2 ** (N - 1))]
seen = 1  

for i in range(2 ** (N - 1)):
  AA = copy.deepcopy(A)
  cost = 0
  s = str(bin(i))[2:]
  if s.count("1") >= K:
    continue
    
  for j in range(N - 1):
    m = max(AA[:j + 1])
    a = AA[j + 1]
    if ((i >> j) & 1) and a <= m:
      cost += (m - a + 1)
      AA[j + 1] = m + 1
  
  if len([AA[i] for i in range(1,N) if AA[i] > max(AA[:i])]) >= K - 1:
    ans[i] = cost
    
    
print(min(ans))

Problem: p03752
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
N,K=map(int,input().split())
A=list(map(int,input().split()))
ans=[]

for i in range(2**(N-1)):
    limit=A[0]
    L=[0]*(N-1)
    total=0
    count=0
    for j in range(N-1):
        if ((i>>j)&1):
            L[j]=1
            count+=1
    if count>=K-1:
        for m in range(N-1):
            if L[m]==1:
                height=max(max(A[:m+1])+1,A[m+1])
                total+=height-A[m+1]
                A[m+1]=height
        ans.append(total)

print(min(ans))

Failing test case input:
5 4
8527 3774 3598 4173 924

Expected output:
14042


Actual output:
0


Fixed submission (verdict: Accepted):
import copy
N,K=map(int,input().split())
A=list(map(int,input().split()))
B=copy.copy(A)
ans=[]

for i in range(2**(N-1)):
    L=[0]*(N-1)
    total=0
    count=0
    for j in range(N-1):
        if ((i>>j)&1):
            L[j]=1
            count+=1
    if count>=K-1:
        A=copy.copy(B)
        for m in range(N-1):
            
            if L[m]==1:
                height=max(max(A[:m+1])+1,A[m+1])
                total+=height-A[m+1]
                A[m+1]=height
                
        ans.append(total)
        

print(min(ans))