結果

問題 No.385 カップ麺生活
ユーザー pekempeypekempey
提出日時 2016-07-01 22:40:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,046 bytes
コンパイル時間 1,211 ms
コンパイル使用メモリ 148,316 KB
実行使用メモリ 4,448 KB
最終ジャッジ日時 2023-07-27 20:46:12
合計ジャッジ時間 2,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,428 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,448 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,428 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T1, class T2> bool chmin(T1 &a, T2 b) { if (b < a) { a = b; return true; } return false; }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; }

int main() {
	vector<bool> is_prime(10101, true);
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i < 10101; i++) {
		if (is_prime[i]) {
			for (int j = i * 2; j < 10101; j += i) {
				is_prime[j] = false;
			}
		}
	}

	int m, n;
	cin >> m >> n;

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

	static int dp[22][10101];
	fill_n(*dp, 22 * 10101, -1e9);
	dp[0][0] = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j <= m; j++) {
			chmax(dp[i + 1][j], dp[i][j]);
			if (j - a[i] >= 0) chmax(dp[i + 1][j], dp[i + 1][j - a[i]] + 1);
		}
	}

	int mx = 0; 
	for (int i = 0; i <= m; i++) {
		mx = max(mx, dp[n][i]);
	}

	long long sum = mx;
	for (int i = 0; i <= m; i++) {
		if (is_prime[m - i]) {
			if (dp[n][i] >= 0) sum += dp[n][i];
		}
	}
	cout << sum << endl;
}
0