int n, m; int d[maxn], a[maxn], tmp[maxn]; int vis[maxn], vis_clock;
boolcheck(int x){ ++vis_clock; int cnt = 0; for (int i = x; i > 0; --i) { if (d[i] != 0 && vis[d[i]] != vis_clock) { tmp[i] = -a[d[i]]; vis[d[i]] = vis_clock; ++cnt; } else { tmp[i] = 1; } } if (cnt != m) { returnfalse; } int sum = 0; for (int i = 1; i <= x; ++i) { sum += tmp[i]; if (sum < 0) { returnfalse; } } returntrue; }
intmain(){ scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d", &d[i]); } for (int i = 1; i <= m; ++i) { scanf("%d", &a[i]); } int L = m, R = n, ans = inf; while (L <= R) { int M = L + (R - L) / 2; if (check(M)) { R = M - 1; ans = min(ans, M); } else { L = M + 1; } } printf("%d\n", ans == inf ? -1 : ans); return0; }