結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー face4face4
提出日時 2019-12-14 10:01:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,144 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 78,660 KB
実行使用メモリ 199,112 KB
最終ジャッジ日時 2023-09-10 11:43:20
合計ジャッジ時間 9,038 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
198,748 KB
testcase_01 AC 83 ms
198,716 KB
testcase_02 AC 83 ms
198,716 KB
testcase_03 AC 83 ms
198,940 KB
testcase_04 AC 83 ms
198,744 KB
testcase_05 AC 82 ms
198,844 KB
testcase_06 AC 83 ms
199,008 KB
testcase_07 AC 82 ms
198,868 KB
testcase_08 AC 83 ms
198,760 KB
testcase_09 AC 95 ms
198,768 KB
testcase_10 AC 55 ms
198,876 KB
testcase_11 AC 53 ms
198,812 KB
testcase_12 AC 71 ms
198,760 KB
testcase_13 AC 123 ms
198,836 KB
testcase_14 AC 63 ms
198,792 KB
testcase_15 AC 61 ms
199,020 KB
testcase_16 AC 67 ms
198,800 KB
testcase_17 AC 73 ms
198,852 KB
testcase_18 AC 51 ms
198,792 KB
testcase_19 AC 51 ms
198,812 KB
testcase_20 AC 51 ms
198,728 KB
testcase_21 AC 52 ms
198,732 KB
testcase_22 AC 51 ms
198,716 KB
testcase_23 AC 51 ms
198,760 KB
testcase_24 AC 148 ms
198,816 KB
testcase_25 AC 153 ms
199,044 KB
testcase_26 AC 145 ms
198,856 KB
testcase_27 AC 145 ms
198,828 KB
testcase_28 AC 136 ms
198,928 KB
testcase_29 AC 138 ms
198,896 KB
testcase_30 AC 50 ms
198,848 KB
testcase_31 AC 50 ms
198,748 KB
testcase_32 AC 51 ms
198,792 KB
testcase_33 AC 52 ms
198,788 KB
testcase_34 AC 52 ms
198,744 KB
testcase_35 AC 147 ms
198,828 KB
testcase_36 AC 149 ms
198,840 KB
testcase_37 AC 152 ms
198,848 KB
testcase_38 AC 153 ms
198,872 KB
testcase_39 AC 153 ms
198,888 KB
testcase_40 AC 152 ms
199,108 KB
testcase_41 AC 144 ms
198,844 KB
testcase_42 AC 145 ms
198,828 KB
testcase_43 AC 143 ms
198,832 KB
testcase_44 AC 144 ms
198,832 KB
testcase_45 AC 144 ms
198,832 KB
testcase_46 AC 137 ms
198,844 KB
testcase_47 AC 137 ms
198,828 KB
testcase_48 AC 137 ms
198,908 KB
testcase_49 AC 137 ms
198,868 KB
testcase_50 AC 137 ms
198,860 KB
testcase_51 AC 138 ms
198,852 KB
testcase_52 AC 112 ms
198,968 KB
testcase_53 AC 122 ms
199,112 KB
testcase_54 AC 112 ms
198,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;

typedef pair<int,int> pii;
int dp[2][5001][5001];

int main(){
    memset(dp, -1, sizeof(dp));
    dp[0][0][0] = 0;
    int n, k;
    cin >> n >> k;
    vector<pii> v;
    for(int i = 0; i < n; i++){
        int p, d;
        cin >> p >> d;
        v.push_back({p, d});
    }
    sort(v.rbegin(), v.rend());
    for(int i = 0; i < n; i++){
        for(int j = k; j >= 0; j--){
            dp[1][i+1][j] = max(dp[1][i+1][j], dp[1][i][j]);
            dp[0][i+1][j] = max(dp[0][i+1][j], dp[0][i][j]);
            if(dp[0][i][j] != -1){
                if(j+v[i].first <= k)   dp[1][i+1][j+v[i].first] = max(dp[1][i+1][j+v[i].first], dp[0][i][j]+v[i].second);
            }
            if(dp[1][i][j] != -1){
                dp[0][i+1][j] = max(dp[0][i+1][j], dp[1][i][j]+v[i].second);
                dp[1][i+1][j] = max(dp[1][i+1][j], dp[1][i][j]);
            }
        }
    }
    int ans = 0;
    for(int i = 0; i <= k; i++){
        ans = max(ans, dp[0][n][i]);
        ans = max(ans, dp[1][n][i]);
    }
    cout << ans << endl;
    return 0;
}
0