This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import sys
from collections import defaultdict
sys.setrecursionlimit(10**6)
n, m = map(int, input().split())
p = [0] * (n + 1)
f = [0] * (m + 1)
used = [[0] * (1 << m) for _ in range(n + 1)]
g = defaultdict(list)
for i in range(1, n + 1):
p[i] = int(input())
for i in range(1, m + 1):
f[i] = int(input())
for y in range(1, (1 << m)):
a = 0
for j in range(m):
if (y >> j) & 1:
a += f[j + 1]
if a <= 1000:
g[a].append(y)
def rec(pos, mask):
if pos == n + 1:
print("YES")
sys.exit(0)
if used[pos][mask]:
return
used[pos][mask] = 1
for i in g[p[pos]]:
if (mask & i) == 0:
rec(pos + 1, mask | i)
rec(1, 0)
print("NO")
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |