結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー HIR180HIR180
提出日時 2019-12-14 00:10:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,905 bytes
コンパイル時間 2,969 ms
コンパイル使用メモリ 193,392 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 07:29:41
合計ジャッジ時間 9,648 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,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 42 ms
4,380 KB
testcase_13 AC 148 ms
4,380 KB
testcase_14 AC 25 ms
4,380 KB
testcase_15 AC 21 ms
4,376 KB
testcase_16 AC 37 ms
4,380 KB
testcase_17 AC 50 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 196 ms
4,376 KB
testcase_25 AC 204 ms
4,380 KB
testcase_26 AC 181 ms
4,380 KB
testcase_27 AC 180 ms
4,376 KB
testcase_28 AC 161 ms
4,380 KB
testcase_29 AC 161 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 5 ms
4,380 KB
testcase_35 AC 196 ms
4,380 KB
testcase_36 AC 197 ms
4,380 KB
testcase_37 AC 203 ms
4,380 KB
testcase_38 AC 204 ms
4,376 KB
testcase_39 AC 205 ms
4,380 KB
testcase_40 AC 180 ms
4,384 KB
testcase_41 AC 179 ms
4,380 KB
testcase_42 AC 179 ms
4,384 KB
testcase_43 AC 177 ms
4,380 KB
testcase_44 AC 179 ms
4,384 KB
testcase_45 AC 177 ms
4,376 KB
testcase_46 AC 155 ms
4,380 KB
testcase_47 AC 159 ms
4,380 KB
testcase_48 AC 162 ms
4,380 KB
testcase_49 AC 160 ms
4,384 KB
testcase_50 AC 160 ms
4,376 KB
testcase_51 AC 163 ms
4,380 KB
testcase_52 AC 135 ms
4,380 KB
testcase_53 AC 149 ms
4,376 KB
testcase_54 AC 86 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
const int INF = 1000000000;
int n,k;
vector<P>vec;
int dp[2][5005],dp2[2][5005];

int main(){
	cin >> n >> k;
	rep(i,n){ int a,b; cin >> a >> b; vec.pb(P(a,b)); }
	SORT(vec);
	reverse(vec.begin(),vec.end());
	rep(i,2)rep(j,5005){
		dp[i][j] = dp2[i][j] = -INF;
	}
	dp[0][0] = 0;
	int cur = 0,nxt = 1;
	rep(i,n){
		rep(j,5005) dp[nxt][j] = dp2[nxt][j] = -INF;
		rep(j,k+1){
			if(dp[cur][j] < 0) continue;
			dp[nxt][j] = max(dp[nxt][j],dp[cur][j]);
			if(j+vec[i].fi <= k){
				dp[nxt][j+vec[i].fi] = max(dp[nxt][j+vec[i].fi],dp[cur][j]+vec[i].sc);
				dp2[nxt][j+vec[i].fi] = max(dp2[nxt][j+vec[i].fi],dp[cur][j]+vec[i].sc);
			}
		}
		rep(j,k+1){
			if(dp2[cur][j] < 0) continue;
			dp2[nxt][j] = max(dp2[nxt][j],dp2[cur][j]);
			dp[nxt][j] = max(dp[nxt][j],dp2[cur][j]+vec[i].sc);
			if(j+vec[i].fi <= k){
				dp2[nxt][j+vec[i].fi] = max(dp2[nxt][j+vec[i].fi],dp2[cur][j]+vec[i].sc);
			}
		}
		swap(cur,nxt);
	}
	ll ans = 0;
	rep(j,k+1){
		ans = max(ans,1LL*dp[cur][j]);
		ans = max(ans,1LL*dp2[cur][j]);
	}
	cout << ans << endl;
}
0