P2214题解
Nuyoah_awa · · 题解
题目大意
有
题目分析
看到
我们定义
对于每种叫声为
知道声音为
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5, INF = 1e9;
int f[N + 5], v[N + 5], n, b, u, now, ans;
int main()
{
scanf("%d %d", &n, &b);
for(int i = 0;i <= N;i++)
f[i] = INF;
f[0] = 0;
for(int i = 1;i <= b;i++)
{
scanf("%d", &v[i]);
for(int j = v[i];j <= N;j++)
f[j] = min(f[j], f[j - v[i]] + 1);
}
for(int i = 1, x;i <= n;i++)
{
scanf("%d", &x);
x -= now, now += x;
now -= now ? 1 : 0;
if(x < 0)
{
printf("-1");
return 0;
}
if(f[x] == INF)
{
printf("-1");
return 0;
}
ans += f[x];
}
printf("%d", ans);
return 0;
}