There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. 

Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k_1 knights with lightsabers of the first color, k_2 knights with lightsabers of the second color, ..., k_{m} knights with lightsabers of the m-th color.

However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.


-----Input-----

The first line of the input contains n (1 ≤ n ≤ 2·10^5) and m (1 ≤ m ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k_1, k_2, ..., k_{m} (with $1 \leq \sum_{i = 1}^{m} k_{i} \leq n$) – the desired counts of Jedi Knights with lightsabers of each color from 1 to m.


-----Output-----

Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output  - 1.


-----Example-----
Input
8 3
3 3 1 2 2 1 1 3
3 1 1

Output
1

Difficulty: interview

Starter code:

Solutions:
from collections import defaultdict


n, m = list(map(int, input().split()))

ns = list(map(int, input().split()))
ms = list(map(int, input().split()))
summs = sum(ms)

target = {
    i: m
    for i, m in enumerate(ms, 1)
}
remain = set(i for i, m in list(target.items()) if m != 0)

count = defaultdict(int)

a = 0
b = 0

while remain and b < n:
    count[ns[b]] += 1
    if ns[b] in remain and target[ns[b]] <= count[ns[b]]:
        remain.remove(ns[b])
    b += 1

if remain:
    print(-1)
else:
    ans = b - summs
    while b <= n:
        if remain:
            if b >= n:
                break
            count[ns[b]] += 1
            if ns[b] in remain and target[ns[b]] <= count[ns[b]]:
                remain.remove(ns[b])
            b += 1
        else:
            count[ns[a]] -= 1
            if target[ns[a]] > count[ns[a]]:
                remain.add(ns[a])
            else:
                ans = min(ans, b - a - 1 - summs)
            a += 1

    print(ans)


n, m = list(map(int, input().split()))
c = list(map(int, input().split()))
k = list(map(int, input().split()))
p = [0] * m
c = [o - 1 for o in c]
s = sum(k)
ok = k.count(0)
i1 = 0
i2 = 0
while i2 < n and ok < m:
    p[c[i2]] += 1
    if p[c[i2]] == k[c[i2]]:
        ok += 1
    i2 += 1
if ok != m:
    print(-1)
    return
ans = i2 - i1
while i1 < n:
    p[c[i1]] -= 1
    while i2 < n and p[c[i1]] < k[c[i1]]:
        p[c[i2]] += 1
        i2 += 1
    if p[c[i1]] >= k[c[i1]]:
        ans = min(ans, i2 - i1 - 1)
    elif i2 == n:
        break
    i1 += 1
print(ans - s)


n, m = list(map(int, input().split()))
c = list(map(int, input().split()))
k = list(map(int, input().split()))
p = [0] * m
c = [i -1 for i in c]
s = sum(k)
cnt = k.count(0)
l = 0
r = 0
while r < n and cnt < m:
    p[c[r]] += 1
    if p[c[r]] == k[c[r]]:
        cnt += 1
    r += 1
if cnt != m:
    print(-1)
    return
ans = r-l
while l < n:
    p[c[l]] -= 1
    while r < n and p[c[l]] < k[c[l]]:
        p[c[r]] += 1
        r += 1
    if p[c[l]] >= k[c[l]]:
        ans = min(ans, r-l-1)
    elif r == n:
        break
    l += 1
print(ans-s)


n, m = map(int, input().split())
c = list(map(int, input().split()))
k = list(map(int, input().split()))
p = [0] * m
c = [i -1 for i in c]
s = sum(k)
cnt = k.count(0)
l = 0
r = 0
while r < n and cnt < m:
    p[c[r]] += 1
    if p[c[r]] == k[c[r]]:
        cnt += 1
    r += 1
if cnt != m:
    print(-1)
    return
ans = r-l
while l < n:
    p[c[l]] -= 1
    while r < n and p[c[l]] < k[c[l]]:
        p[c[r]] += 1
        r += 1
    if p[c[l]] >= k[c[l]]:
        ans = min(ans, r-l-1)
    elif r == n:
        break
    l += 1
print(ans-s)

import random, math
from copy import deepcopy as dc
from bisect import bisect_left, bisect_right



# Function to call the actual solution
def solution(li, li1):
	freq = {}
	for i in range(len(li1)):
		if li1[i]:
			freq[i+1] = li1[i]
	
	maxi = len(li)
	# Exclusive ranges j - i 
	i = 0
	j = 0
	req = dc(freq)
	cur_freq = [0]*(len(li1) + 1)
	flag = False
	while i < len(li):
		while len(req) and j < len(li):
			cur_freq[li[j]] += 1
			if li[j] in req:
				req[li[j]] -= 1
				if req[li[j]] <= 0:
					del req[li[j]]
			j += 1
		if len(req):
			break
		flag = True
		maxi = min(maxi, j - i)
		cur_freq[li[i]] -= 1
		if li[i] in freq and cur_freq[li[i]] < freq[li[i]]:
			req[li[i]] = req.get(li[i], 0) + 1
		i += 1
	if not flag:
		return -1
	return maxi - sum(li1)



# Function to take input
def input_test():
	# for _ in range(int(input())):
		# n = int(input())
		a, b = list(map(int, input().strip().split(" ")))
		# a, b, c = map(int, input().strip().split(" "))
		li = list(map(int, input().strip().split(" ")))
		li1 = list(map(int, input().strip().split(" ")))
		out = solution(li, li1)
		print(out)

# Function to check test my code
def test():
	pass


input_test()
# test()


n,m=list(map(int,input().split()))   
arr=list(map(int,input().split()))   
marr=list(map(int,input().split()))  
f=[0]*(m+1)
cnt,i=0,0
valid=sum(marr)   #m
while(i<n):
    f[arr[i]]+=1
    if f[arr[i]]<=marr[arr[i]-1]:
        cnt+=1
    if cnt==valid:
        break
    i+=1
if i==n:
    print(-1)
else:
    ans=(i+1)-valid
    s,e=0,i
    while(e<n):
        while(f[arr[s]]>marr[arr[s]-1]):
            f[arr[s]]-=1
            s+=1
        ans=min((e-s+1)-valid,ans)
        e+=1
        if e<n:
            f[arr[e]]+=1
    print(ans)