Problem:


Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.  

Constraints

1≦N≦40 

1≦a_i,b_i≦10 

1≦c_i≦100 

1≦M_a,M_b≦10 

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b  
a_1 b_1 c_1  
a_2 b_2 c_2
:  
a_N b_N c_N  

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.  

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.



Sample input:
3 1 1
1 2 1
2 1 2
3 3 10


Sample output:
3

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]

inf = float('inf')
dp = [[[inf] * 401 for _ in range(401)] for _ in range(N + 1)]

dp[0][0][0] = 0

for i in range(N):
    for j in range(401):
        for k in range(401):
            dp[i + 1][j][k] = dp[i][k][j]
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j - abc[i][0]][k - abc[i][1]] + abc[i][2])
ans = inf
for a in range(1, 401):
    for b in range(1, 401):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]

inf = float('inf')
dp = [[[inf] * 401 for _ in range(401)] for _ in range(N + 1)]

dp[0][0][0] = 0

for i in range(N):
    for j in range(401):
        for k in range(401):
            dp[i + 1][j][k] = dp[i][k][j]
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j - abc[i][0]][k - abc[i][1]] + abc[i][2])
ans = inf
for a in range(1, 401):
    for b in range(1, 401):
        if a * Ma == b * Mb:
            ans = min(ans, dp[N][a][b])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]

inf = float('inf')
dp = [[[inf] * 401 for _ in range(401)] for _ in range(N + 1)]

dp[0][0][0] = 0

for i in range(N):
    for j in range(401):
        for k in range(401):
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i + 1][j][k] = min(dp[i][j][k], dp[i][j - abc[i][0]][k - abc[i][1]] + abc[i][2])
            else:
                dp[i + 1][j][k] = dp[i][k][j]
ans = inf
for a in range(1, 401):
    if a % Ma == 0:
        b = a // Ma * Mb
        if b <= 401:
            ans = min(ans, dp[N][a][b])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]

inf = float('inf')
dp = [[[inf] * 401 for _ in range(401)] for _ in range(N + 1)]

dp[0][0][0] = 0

for i in range(N):
    for j in range(401):
        for k in range(401):
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i + 1][j][k] = min(dp[i][j][k], dp[i][j - abc[i][0]][k - abc[i][1]] + abc[i][2])
ans = inf
for a in range(1, 401):
    if a % Ma == 0:
        b = a // Ma * Mb
        if b <= 401:
            ans = min(ans, dp[N][a][b])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]

inf = float('inf')
dp = [[[inf] * 401 for _ in range(401)] for _ in range(N + 1)]

dp[0][0][0] = 0

for i in range(N):
    for j in range(401):
        for k in range(401):
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i + 1][j][k] = min(dp[i][j][k], dp[i][j - abc[i][0]][k - abc[i][1]] + abc[i][2])
ans = inf
for a in range(401):
    if a % Ma == 0:
        b = a // Ma * Mb
        if b <= 401:
            ans = min(ans, dp[N][a][b])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]

inf = float('inf')
dp = [[[inf] * 401 for _ in range(401)] for _ in range(N + 1)]

dp[0][0][0] = 0

for i in range(N - 1):
    for j in range(401):
        for k in range(401):
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i + 1][j][k] = min(dp[i][j][k], dp[i][j - abc[i][0]][k - abc[i][1]] + abc[i][2])
ans = inf
for a in range(401):
    if a % Ma == 0:
        b = a // Ma * Mb
        if b <= 401:
            ans = min(ans, dp[N][a][b])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[float('inf')]*401 for i in range(401)]
make = [[0,0]]
dp[0][0] = 0
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        if dp[t+a][u+b] == float('inf'):
            make.append([t+a, u+b])
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
ans = float('inf')
for i in range(1,401):
    for j in range(1,401):
        if i*mb == j*ma:
            ans = min(ans, dp[i][j])
if ans == float('inf'):
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = []
for i in range(401):
    d = [10**5]*401
    dp.append(d)
make = [[0,0]]
dp[0][0] = 0
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        if dp[t+a][u+b] == 10**5:
            make.append([t+a, u+b])
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
ans = 10**5
for i in range(1,401):
    for j in range(1,401):
        if i*mb == j*ma:
            ans = min(ans, dp[i][j])
if ans == 10**5:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[10**5]*401 for i in range(401)]
make = [[0,0]]
dp[0][0] = 0
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        if dp[t+a][u+b] == 10**5:
            make.append([t+a, u+b])
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
ans = 10**5
for i in range(1,401):
    for j in range(1,401):
        if i*mb == j*ma:
            ans = min(ans, dp[i][j])
if ans == 10**5:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[10**5]*405 for i in range(405)]
make = [[0,0]]
dp[0][0] = 0
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        if dp[t+a][u+b] == 10**5:
            make.append([t+a, u+b])
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
if ma <= mb:
    cnt = 1
    ans = 10**5
    while mb*cnt <= 404:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)
else:
    cnt = 1
    ans = 10**5
    while ma*cnt <= 404:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[10**5]*401 for i in range(401)]
make = [[0,0]]
dp[0][0] = 0
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        if dp[t+a][u+b] == 10**5:
            make.append([t+a, u+b])
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
if ma <= mb:
    cnt = 1
    ans = 10**5
    while mb*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)
else:
    cnt = 1
    ans = 10**5
    while ma*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[10**5]*401 for i in range(401)]
make = [[0,0],[L[0][0],L[0][1]]]
dp[L[0][0]][L[0][1]] = L[0][2]
dp[0][0] = 0
for i in range(1,n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        if dp[t+a][u+b] == 10**5:
            make.append([t+a, u+b])
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
if ma <= mb:
    cnt = 1
    ans = 10**5
    while mb*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)
else:
    cnt = 1
    ans = 10**5
    while ma*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[10**5]*401 for i in range(401)]
make = [[0,0],[L[0][0],L[0][1]]]
dp[L[0][0]][L[0][1]] = L[0][2]
dp[0][0] = 0
for i in range(1,n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
        make.append([t+a, u+b])
if ma <= mb:
    cnt = 1
    ans = 10**5
    while mb*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)
else:
    cnt = 1
    ans = 10**5
    while ma*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)

    def get(self, init=None):
        if self.v is None:
            return init
        else:
            return self.v


def main():
    N, MA, MB = read_values()
    dp = [
        [[10 ** 20 for b in range(N * 10 + 1)] for a in range(N * 10 + 1)]
        for n in range(N + 1)
    ]

    dp[0][0][0] = 0

    for n in range(1, N + 1):
        a, b, c = read_values()
        for aa in range(N * 10):
            for bb in range(N * 10 - b):
                if aa - a < 0 or bb - b < 0:
                    dp[n][aa][bb] = dp[n - 1][aa][bb]
                else:
                    dp[n][aa][bb] = min(
                        dp[n - 1][aa - a][bb - b] + c, dp[n - 1][aa][bb]
                    )

    res = V(min)
    max_k = (10 * N) // max(MA, MB)
    for k in range(1, max_k + 1):
        res.ud(dp[N - 1][MA * k][MB * k])

    print(res if res.get() < 10 ** 20 else -1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import defaultdict

    inf = 100 * 40 + 1

    N, Ma, Mb = map(int, input().split())

    size = 400 * N * 2 + 10
    dp = [inf] * size
    # dp[x]:=minimum price

    # (b-a)(Mb+Ma)-(b+a)(Mb-Ma)=0を目指す
    for _ in range(N):
        a, b, p = map(int, input().split())
        x = (b - a) * (Mb + Ma) - (b + a) * (Mb - Ma)
        for key in range(size):
            if dp[key] == inf: continue
            nkey = (key + x) % size
            dp[nkey] = min(dp[nkey], dp[key] + p)
        dp[x] = min(dp[x], p)

    if dp[0] == inf:
        print(-1)
    else:
        print(dp[0])


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
def main():
    from collections import defaultdict

    inf = 100 * 40 + 1

    N, Ma, Mb = map(int, input().split())

    dp = defaultdict(lambda: inf)
    # dp[x]:=minimum price

    # (b-a)(Mb+Ma)-(b+a)(Mb-Ma)=0を目指す
    for _ in range(N):
        a, b, p = map(int, input().split())
        x = (b - a) * (Mb + Ma) - (b + a) * (Mb - Ma)
        for key in tuple(dp.keys()):
            dp[key + x] = min(dp[key + x], dp[key] + p)
        dp[x] = min(dp[x], p)

    if dp[0] == inf:
        print(-1)
    else:
        print(dp[0])


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
def main():
    inf = 100 * 40 + 1
    N, Ma, Mb = map(int, input().split())

    dpa = [inf] * (400 + 1)
    dpa[0] = 0
    dpb = [inf] * (400 + 1)
    dpb[0] = 0
    # dp[x]:=物質をxg用意する最小費用

    for _ in range(N):
        a, b, price = map(int, input().split())
        for g in range(400, -1, -1):
            dpa[g] = min(dpa[g], dpa[g - a] + price)
            dpb[g] = min(dpb[g], dpb[g - b] + price)

    ans = inf
    k = 1
    while max(Ma * k, Mb * k) <= 400:
        cost = dpa[Ma * k] + dpb[Mb * k]
        if cost < ans:
            ans = cost
        k += 1

    if ans == inf:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]
R = 500

dp = [[10**18] * R for _ in range(R)]
dp[0][0] = 0

for a, b, c in ABC:
    for x in range(R):
        for y in range(R):
            if x + a < R and y + b < R:
                dp[x + a][y + b] = min(dp[x + a][y + b], dp[x][y] + c)

ans = 10**18
for x in range(1, R):
    for y in range(1, R):
        if B * x == A * y:
            ans = min(ans, dp[x][y])
print(ans if ans < 10**18 else -1)

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]
R = 500

dp = [[10**18] * R for _ in range(R)]
dp[0][0] = 0

for x in range(R):
    for y in range(R):
        for a, b, c in ABC:
            if x + a < R and y + b < R:
                dp[x + a][y + b] = min(dp[x + a][y + b], dp[x][y] + c)

ans = 10**18
for x in range(R):
    if (B * x) % A != 0:
        continue
    y = B * x // A
    if x + y == 0 or y >= R or x >= R:
        continue
    ans = min(ans, dp[x][y])
print(ans if ans < 10**18 else -1)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
dp = [[4001]*401 for _ in range(401)]
dp[0][0] = 0
for i in range(n):
    a,b,c = map(int,input().split())
    for j in range(401):
        for k in range(401):
            if a+j <= 400 and b+k <= 400:
                dp[a+j][b+k] = min(dp[a+j][b+k],dp[j][k] + c)
m = 4001
for i in range(1,401):
    if ma*i <= 400 and mb*i <= 400:
        m = min(m,dp[ma*i][mb*i])
if m == 4001:
    print(-1)
else:
    print(m)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(N)]

an = 0
bn = 0
for a, b, c in abc:
  an += a
  bn += b

ma = max(an, bn) + 1

DP = [[10 ** 10] * ma for _ in range(ma)]

for i in range(N):
  a, b, c = abc[i]
  for j in range(ma):
    for k in range(ma):
      if j == 0 and k == 0:
        DP[a][b] = min(DP[a][b], c)
        continue
      if DP[j][k] == 10 ** 10: continue
      if j + a >= ma or k + b >= ma: continue
      DP[j + a][k + b] = min(DP[j + a][k + b], DP[j][k] + c)

ans = 10 ** 10
a, b = Ma, Mb
while a < ma and b < ma:
  ans = min(ans, DP[a][b])
  a += Ma
  b += Mb

if ans == 10 ** 10:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
dp = [[4001]*1001 for _ in range(1001)]
dp[0][0] = 0
for i in range(n):
    a,b,c = map(int,input().split())
    for j in range(1001):
        for k in range(1001):
            if a+j <= 1000 and b+k <= 1000:
                dp[a+j][b+k] = min(dp[a+j][b+k],dp[j][k] + c)
m = 4001
for i in range(1,1000):
    if ma*i <= 1000 and mb*i <= 1000:
        m = min(m,dp[ma*i][mb*i])
if m == 4001:
    print(-1)
else:
    print(m)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    inf=10**9
    n,ma,mb=MI()
    vv=[]
    cc=[]
    for _ in range(n):
        a,b,c=MI()
        vv.append(a*mb-b*ma)
        cc.append(c)
    dp=[[inf]*8005 for _ in range(n+1)]
    dp[0][4000]=0
    for i in range(n):
        v,c=vv[i],cc[i]
        for j in range(8005):
            pre=dp[i][j]
            if pre==inf:continue
            dp[i + 1][j] = min(dp[i + 1][j], pre)
            dp[i + 1][j + v] = min(dp[i + 1][j + v], pre + c)
    ans=inf
    dpi=dp[-1]
    for j in range(1,4000):
        ans=min(ans,dpi[4000-j]+dpi[4000+j])
    if ans==inf:print(-1)
    else:print(ans)

main()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,A,B = LI()
    G = LIR(n)
    ma = sum([a for (a,_,_) in G])
    mb = sum([b for (_,b,_) in G])
    dp = [[float("inf")]*(mb+1) for i in range(ma+1)]
    dp[0][0] = 0
    for sa in range(ma)[::-1]:
        for sb in range(mb)[::-1]:
            dpsasb = dp[sa][sb]
            for a,b,c in G:
                na = sa+a
                if na > ma:
                    continue
                nb = sb+b
                if nb > mb:
                    continue
                nd = dpsasb+c
                if nd < dp[na][nb]:
                    dp[na][nb] = nd
    ans = float("inf")
    for i in range(1,ma+1):
        s = B*i
        if s%A:
            continue
        j = B*i//A
        if j > mb:
            continue
        if dp[i][j] < ans:
            ans = dp[i][j]
    if ans == float("inf"):
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,A,B = LI()
    G = LIR(n)
    ma = sum([a for (a,_,_) in G])
    mb = sum([b for (_,b,_) in G])
    dp = [[float("inf")]*(mb+1) for i in range(ma+1)]
    dp[0][0] = 0
    for sa in range(ma):
        for sb in range(mb):
            dpsasb = dp[sa][sb]
            for a,b,c in G:
                na = sa+a
                if na > ma:
                    continue
                nb = sb+b
                if nb > mb:
                    continue
                nd = dpsasb+c
                if nd < dp[na][nb]:
                    dp[na][nb] = nd
    ans = float("inf")
    for i in range(1,ma+1):
        s = B*i
        if s%A:
            continue
        j = B*i//A
        if j > mb:
            continue
        if dp[i][j] < ans:
            ans = dp[i][j]
    if ans == float("inf"):
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def gcd(a,b):
        if a == b == 0:
            return 1
        if a == 0:
            return b
        return gcd(b%a, a)

    n,A,B = LI()
    G = LIR(n)
    n1 = n>>1
    n2 = n-n1
    s1 = defaultdict(lambda : float("inf"))
    s2 = defaultdict(lambda : float("inf"))
    for j in range(1<<n1):
        sa = 0
        sb = 0
        sc = 0
        for i in range(n1):
            if j&(1<<i):
                a,b,c = G[i]
                sa += a
                sb += b
                sc += c
        g = gcd(sa,sb)
        sa //= g
        sb //= g
        if s1[(sa,sb)] > sc:
            s1[(sa,sb)] = sc
    m = 1
    for j in range(1<<n2):
        sa = 0
        sb = 0
        sc = 0
        for i in range(n2):
            if j&(1<<i):
                a,b,c = G[i+n1]
                sa += a
                sb += b
                sc += c
        g = gcd(sa,sb)
        sa //= g
        sb //= g
        if sa > m:
            m = sa
        if s2[(sa,sb)] > sc:
            s2[(sa,sb)] = sc
    ans = float("inf")
    for (a,b),c in s1.items():
        for i in range(m+1):
            s = B*(i+a)
            if s%A:
                continue
            j = (s//A)-b
            nc = s2[(i,j)]
            cost = c+nc
            if a == b == i == j:
                continue
            if cost < ans:
                ans = cost
    if ans == float("inf"):
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    inf=10**9
    n,ma,mb=MI()
    vv=[]
    cc=[]
    for _ in range(n):
        a,b,c=MI()
        vv.append(a*mb-b*ma)
        cc.append(c)

    pp=[(0,0)]
    for i in range(n//2):
        v=vv[i]
        c=cc[i]
        np=[]
        for pv,pc in pp:np.append((v+pv,c+pc))
        pp+=np
    pp.sort()
    pre=[]
    for v,c in pp:
        if pre and pre[-1][0]==v:continue
        pre.append((v,c))

    pp=[(0,0)]
    for i in range(n//2,n):
        v=vv[i]
        c=cc[i]
        np=[]
        for pv,pc in pp:np.append((v+pv,c+pc))
        pp+=np
    pp.sort()
    pos=[]
    for v,c in pp:
        if pos and pos[-1][0]==v:continue
        pos.append((v,c))

    j=len(pos)-1
    ans=inf
    for v,c in pre:
        while j and v+pos[j][0]>0:j-=1
        if v+pos[j][0]==0 and c+pos[j][1]>0:ans=min(ans,c+pos[j][1])

    if ans==inf:print(-1)
    else:print(ans)

main()

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    inf=10**9
    n,ma,mb=MI()
    vv=[]
    cc=[]
    for _ in range(n):
        a,b,c=MI()
        vv.append(a*mb-b*ma)
        cc.append(c)

    pp=[(0,0)]
    for i in range(n//2):
        v=vv[i]
        c=cc[i]
        np=[]
        for pv,pc in pp:np.append((v+pv,c+pc))
        pp+=np
    pp.sort()
    pre=[]
    for v,c in pp:
        if pre and pre[-1][0]==v:continue
        pre.append((v,c))

    pp=[(0,0)]
    for i in range(n//2,n):
        v=vv[i]
        c=cc[i]
        np=[]
        for pv,pc in pp:np.append((v+pv,c+pc))
        pp+=np
    pp.sort()
    pos=[]
    for v,c in pp:
        if pos and pos[-1][0]==v:continue
        pos.append((v,c))

    print(pre,pos)
    j=len(pos)-1
    ans=inf
    for v,c in pre:
        while j and v+pos[j][0]>0:j-=1
        if v+pos[j][0]==0 and c+pos[j][1]>0:ans=min(ans,c+pos[j][1])

    if ans==inf:print(-1)
    else:print(ans)

main()

Submission status: Wrong Answer

Solution code:
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    inf=10**9
    n,ma,mb=MI()
    vv=[]
    cc=[]
    for _ in range(n):
        a,b,c=MI()
        vv.append(a*mb-b*ma)
        cc.append(c)

    pp=[(0,0)]
    for i in range(n//2):
        v=vv[i]
        c=cc[i]
        np=[]
        for pv,pc in pp:np.append((v+pv,c+pc))
        pp+=np
    pp.sort()
    pre=[]
    for v,c in pp:
        if pre and pre[-1][0]==v:continue
        pre.append((v,c))

    pp=[(0,0)]
    for i in range(n//2,n):
        v=vv[i]
        c=cc[i]
        np=[]
        for pv,pc in pp:np.append((v+pv,c+pc))
        pp+=np
    pp.sort()
    pos=[]
    for v,c in pp:
        if pos and pos[-1][0]==v:continue
        pos.append((v,c))
    j=len(pos)-1
    ans=inf
    for v,c in pre:
        while j and v+pos[j][0]>0:j-=1
        if v+pos[j][0]==0 and c+pos[j][1]>0:ans=min(ans,c+pos[j][1])

    if ans==inf:print(-1)
    else:print(ans)

main()

Submission status: Wrong Answer

Solution code:
n,MA,MB=map(int,input().split())
INF=10**10
A=[]
B=[]
C=[]
for i in range(n):
  a,b,c=map(int,input().split())
  if MA*b-MB*a>0:
    A.append([MA*b-MB*a,c])
  elif MA*b-MB*a<0:
    B.append([MB*a-MA*b,c])
  else:
    C.append(c)
dp1=[[INF]*10010 for i in range(len(A)+1)]
for i in range(1,len(A)+1):
  for j in range(1,10010):
    if j<A[i-1][0]:
      dp1[i][j]=dp1[i-1][j]
    elif j==A[i-1][0]:
      dp1[i][j]=min(A[i-1][1],dp1[i-1][j])
    else:
      dp1[i][j]=min(dp1[i-1][j],dp1[i-1][j-A[i-1][0]]+A[i-1][1])
dp2=[[INF]*10010 for i in range(len(B)+1)]
for i in range(1,len(B)+1):
  for j in range(1,10010):
    if j<B[i-1][0]:
      dp2[i][j]=dp2[i-1][j]
    elif j==B[i-1][0]:
      dp2[i][j]=min(B[i-1][1],dp2[i-1][j])
    else:
      dp2[i][j]=min(dp2[i-1][j],dp2[i-1][j-B[i-1][0]]+B[i-1][1])
ans=INF
for i in range(10010):
  a=dp1[-1][i]+dp2[-1][i]
  if a<ans:
    ans=a
for i in range(len(C)):
  if c<ans:
    ans=c
if ans<INF:
  print(ans)
else:
  print(-1)