-
Notifications
You must be signed in to change notification settings - Fork 2
/
M.py
55 lines (40 loc) · 1008 Bytes
/
M.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
sys.setrecursionlimit(1000000000)
def dfs(i, j, e, rem):
# print(i, j, e, rem)
visited.add((i, j))
if (i < L) and ((i + 1, j) not in visited) and (left[i] <= rem):
if left[i] == rem:
new_e = e + 1
if new_e == E:
return True
new_rem = enemies[new_e]
else:
new_e = e
new_rem = rem - left[i]
if dfs(i + 1, j, new_e, new_rem):
return True
if (j < R) and ((i, j + 1) not in visited) and (right[j] <= rem):
if right[j] == rem:
new_e = e + 1
if new_e == E:
return True
new_rem = enemies[new_e]
else:
new_e = e
new_rem = rem - right[j]
if dfs(i, j + 1, new_e, new_rem):
return True
return False
E, L, R = map(int, input().strip().split())
enemies = list(map(int, input().strip().split()))
left = list(map(int, input().strip().split()))
right = list(map(int, input().strip().split()))
if (sum(left) + sum(right)) < sum(enemies):
print("NO")
else:
visited = set()
if dfs(0, 0, 0, enemies[0]):
print("YES")
else:
print("NO")