class Edge: def __init__(self, to, cap, rev): self.to = to self.cap = cap self.rev = rev class Dinic: def __init__(self, V): self.V = V self.size = [0] * V """:type : list[int]""" self.level = [] """:type : list[int]""" self.iterator = [] """:type : list[int]""" self.G = [[] for i in range(V)] """:type : list[list[Edge]]""" def add_edge(self, _from, to, cap): self.G[_from].append(Edge(to, cap, self.size[to])) self.G[to].append(Edge(_from, 0, self.size[_from])) self.size[_from] += 1 self.size[to] += 1 def bfs(self, s): from collections import deque level = [-1 for i in range(self.V)] level[s] = 0 q = deque([s]) while q: v = q.popleft() for u in self.G[v]: if u.cap > 0 > level[u.to]: level[u.to] = level[v] + 1 q.append(u.to) return level def dfs(self, v, t, f): if v == t: return f for i in range(self.iterator[v], self.size[v]): self.iterator[v] = i edge = self.G[v][i] if edge.cap > 0 and self.level[v] < self.level[edge.to]: d = self.dfs(edge.to, t, f if f < edge.cap else edge.cap) if d > 0: self.G[v][i].cap -= d self.G[edge.to][edge.rev].cap += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.level = self.bfs(s) if self.level[t] < 0: return flow self.iterator = [0 for i in range(self.V)] while True: f = self.dfs(s, t, float('inf')) if f == 0: break flow += f def solve(): inf = float('inf') W = int(input()) N = int(input()) J = list(map(int, input().split())) M = int(input()) C = list(map(int, input().split())) xs = [] for i in range(M): q = list(map(int, input().split())) for k in q[1:]: xs.append((k - 1, i)) dinic = Dinic(2 + N + M) xs = set(xs) for n in range(N): for m in range(M): if (n, m) not in xs: dinic.add_edge(2 + n, 2 + N + m, inf) for n in range(N): dinic.add_edge(0, 2 + n, J[n]) for m in range(M): dinic.add_edge(2 + N + m, 1, C[m]) mf = dinic.max_flow(0, 1) print('SHIROBAKO' if mf >= W else 'BANSAKUTSUKITA') if __name__ == '__main__': solve()