結果

問題 No.247 線形計画問題もどき
ユーザー masa
提出日時 2015-07-21 00:57:18
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 504 bytes
コンパイル時間 469 ms
コンパイル使用メモリ 60,352 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-07 11:08:33
合計ジャッジ時間 1,520 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

const int INF = 1e7;

int main() {
	int c, n;

	cin >> c >> n;
	vector<int> a(n, 0);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	vector<int> dp(1e5 * 2 + 1, INF);
	dp[0] = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < c; j++) {
			if (dp[j] != INF) {
				dp[j + a[i]] = min(dp[j + a[i]], dp[j] + 1);
			}
		}
	}

	if (dp[c] == INF) {
		cout << -1 << endl;
	} else {
		cout << dp[c] << endl;
	}

	return 0;
}
0