求助80
查看原帖
求助80
235901
Always_Remember_It楼主2022/8/18 17:50
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fo(a,b,c) for(int a=b;a<=c;a++)
#define of(a,b,c) for(int a=b;a>=c;a--)
const int N=35;
const int M=2010;
int a[N];
int cnt;
bool vis[M];
bool cmp(int a,int b){
	return a<b;
}
inline int read(){
	int s=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();		
	}
	while(ch>='0'&&ch<='9'){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*f;
}
signed main(){
    vis[0]=1;
	int x,n;
	x=read();
	n=read();
	fo(i,1,n){
		a[i]=read();
	}
	stable_sort(a+1,a+n+1,cmp);
	if(a[1]!=1){
		cout<<-1<<endl;
		return 0;
	}
	cnt+=a[2]-1;
	fo(i,1,a[2]-1){
		vis[i]=1;
	}
	fo(i,2,n){
		while(1){
			bool f=1;
			int s=0;
			if(i==n){
				s=x;
			}
			else s=a[i+1]-1;
			fo(j,a[i],s){
				if(!vis[j]){
					f=0;
					break;
				}
			}
			if(f) break;
			of(j,s,0){
				if(vis[j]){
					vis[j+a[i]]=1;
				}
			}
			++cnt;
		}	
	}
	cout<<cnt<<endl;
	return 0; 
}
2022/8/18 17:50
加载中...