結果

問題 No.247 線形計画問題もどき
ユーザー 158b158b
提出日時 2015-07-17 23:24:34
言語 C++11
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 944 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 77,872 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-07-08 09:33:08
合計ジャッジ時間 3,976 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 5
other AC * 2 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
#include <map>
#include <bitset>
using namespace std;

//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};

int x[100] = {0};
int total,n;
int a[100] = {0};

bool func(int op, int left, int &ans){
	if(left == 0){
		ans = min(ans, accumulate(x, x+n, 0));
		return true;
	}
	
	if(op >= n){
		return false;
	}
	
	x[op] = left / a[op];
	
	while(x[op] >= 0){
		func(op+1, left - a[op] * x[op], ans);
		
		x[op] --;
	}
	x[op] = 0;
	return false;
}

int main(){
	int ans = INT_MAX;
	
	cin >> total >> n;
	rep(i,n){
		cin >> a[i];
	}
	
	sort(a, a + n, greater<int>());  	//降順
	
	func(0, total, ans);
	cout << (ans == INT_MAX ? -1 : ans) << endl;
	
    return 0;
}
0