結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー startcppstartcpp
提出日時 2019-12-14 00:28:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,179 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 69,304 KB
実行使用メモリ 199,048 KB
最終ジャッジ日時 2023-09-10 07:58:19
合計ジャッジ時間 6,744 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 7 ms
16,688 KB
testcase_10 AC 5 ms
10,460 KB
testcase_11 AC 6 ms
16,532 KB
testcase_12 AC 32 ms
48,368 KB
testcase_13 AC 114 ms
185,336 KB
testcase_14 AC 19 ms
30,008 KB
testcase_15 AC 21 ms
41,760 KB
testcase_16 AC 35 ms
78,268 KB
testcase_17 AC 61 ms
178,172 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 4 ms
7,148 KB
testcase_22 AC 3 ms
5,984 KB
testcase_23 AC 3 ms
6,276 KB
testcase_24 AC 140 ms
198,700 KB
testcase_25 AC 140 ms
198,696 KB
testcase_26 AC 139 ms
198,696 KB
testcase_27 AC 139 ms
198,940 KB
testcase_28 AC 133 ms
198,696 KB
testcase_29 AC 133 ms
199,048 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 3 ms
6,124 KB
testcase_33 AC 4 ms
6,436 KB
testcase_34 AC 5 ms
7,412 KB
testcase_35 AC 138 ms
198,752 KB
testcase_36 AC 138 ms
198,748 KB
testcase_37 AC 138 ms
198,704 KB
testcase_38 AC 138 ms
198,748 KB
testcase_39 AC 139 ms
198,696 KB
testcase_40 AC 138 ms
198,812 KB
testcase_41 AC 139 ms
198,820 KB
testcase_42 AC 139 ms
198,740 KB
testcase_43 AC 140 ms
198,768 KB
testcase_44 AC 140 ms
198,744 KB
testcase_45 AC 138 ms
198,772 KB
testcase_46 AC 133 ms
198,792 KB
testcase_47 AC 133 ms
198,844 KB
testcase_48 AC 134 ms
198,768 KB
testcase_49 AC 134 ms
198,768 KB
testcase_50 AC 133 ms
198,760 KB
testcase_51 AC 133 ms
198,796 KB
testcase_52 AC 121 ms
198,764 KB
testcase_53 AC 138 ms
198,924 KB
testcase_54 AC 128 ms
198,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//一度に買ってから無料にするピザを選ぶと考えても良い。
//買うピザの集合を決めたとき、2,4,6,…番目に高いピザをタダにするべき。
//ピザを値段の高い順にソートして「取る、取らない」を考える。このとき、偶数枚目は無料になると考えれば、
//普通のナップザック問題になる。
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

typedef pair<int, int> P;

void chmax(int &a, int b) { a = max(a, b); }

int n, K;
P pd[5000];
int dp[5001][5001][2];

int main() {
	int i, j, k;
	
	cin >> n >> K;
	rep(i, n) cin >> pd[i].first >> pd[i].second;
	sort(pd, pd + n, greater<P>());
	
	rep(i, n + 1) rep(j, K + 1) rep(k, 2) dp[i][j][k] = -1e+9;
	dp[0][0][0] = 0;
	
	rep(i, n) {
		rep(j, K + 1) {
			rep(k, 2) {
				chmax(dp[i + 1][j][k], dp[i][j][k]);
				int w = j + pd[i].first * (k == 0);
				if (w <= K) {
					chmax(dp[i + 1][w][!k], dp[i][j][k] + pd[i].second);
				}
			}
		}
	}
	
	int ans = -1e+9;
	rep(j, K + 1) rep(k, 2) chmax(ans, dp[n][j][k]);
	cout << ans << endl;
	return 0;
}
0