查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+5;
int a[maxn],b[maxn],c[maxn],tot,d[maxn];
struct BIT
{
int bit[maxn];
BIT(){memset(bit,-0x3f,sizeof(bit));}
void modify(int n,int x,int val){
for(int i = x;i <= n;i += i&-i)bit[i] = max(bit[i],val);
}
int query(int x){
int ans = -1e9;
for(int i = x;i;i -= i&-i)ans = max(ans,bit[i]);
return ans;
}
void goback(int n,int x){
for(int i = x;i <= n;i += i&-i)bit[i]=-1e9;
}
}bit;
int solve(int l,int r)
{
int tl = l+1;
int tr = r-1;
if(tl>tr)return 0;
int ans = 0;
int v = lower_bound(c,c+tot,a[l]-l)-c+1;
bit.modify(tot+1,v,0);
for(int i = tl;i <= r;++i){
int val = lower_bound(c,c+tot,a[i]-i)-c+1;
int tmp = bit.query(val);
bit.modify(tot+1,val,tmp+1);
if(i==r)ans = max(0,tmp);
}
for(int i = l;i <= r;++i){
int val = lower_bound(c,c+tot,a[i]-i)-c+1;
bit.goback(tot+1,val);
}
return (r-l-1-ans);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
a[0] = -1e9;
a[n+1] = 2e9;
for(int i = 1;i <= n;++i)scanf("%d",&a[i]),c[i]=a[i]-i;
c[n+1] = a[n+1]-n-1;
c[0] = -1e9;
sort(c,c+2+n);
tot = unique(c,c+2+n)-c;
for(int i = 1;i <= m;++i)scanf("%d",&b[i]);
for(int i = 2;i <= m;++i){
if(a[b[i]]-a[b[i-1]]<b[i]-b[i-1]){
printf("-1\n");
return 0;
}
}
int ans = 0;
int last = 0;
for(int i = 1;i <= m;++i){
ans += solve(last,b[i]);
last = b[i];
}
ans += solve(last,n+1);
cout<<ans<<endl;
return 0;
}