結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー outlineoutline
提出日時 2021-01-14 04:50:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 1,312 bytes
コンパイル時間 984 ms
コンパイル使用メモリ 84,196 KB
実行使用メモリ 199,168 KB
最終ジャッジ日時 2024-05-02 21:05:44
合計ジャッジ時間 7,912 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 8 ms
9,088 KB
testcase_10 AC 5 ms
6,528 KB
testcase_11 AC 5 ms
7,424 KB
testcase_12 AC 46 ms
46,336 KB
testcase_13 AC 162 ms
158,336 KB
testcase_14 AC 26 ms
28,288 KB
testcase_15 AC 27 ms
30,976 KB
testcase_16 AC 43 ms
46,720 KB
testcase_17 AC 61 ms
65,536 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 5 ms
6,272 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 213 ms
199,168 KB
testcase_25 AC 220 ms
199,040 KB
testcase_26 AC 217 ms
199,168 KB
testcase_27 AC 216 ms
199,168 KB
testcase_28 AC 206 ms
199,168 KB
testcase_29 AC 213 ms
199,040 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 4 ms
5,632 KB
testcase_34 AC 6 ms
7,424 KB
testcase_35 AC 209 ms
199,168 KB
testcase_36 AC 207 ms
199,040 KB
testcase_37 AC 211 ms
199,040 KB
testcase_38 AC 213 ms
199,168 KB
testcase_39 AC 219 ms
199,168 KB
testcase_40 AC 209 ms
199,168 KB
testcase_41 AC 210 ms
199,040 KB
testcase_42 AC 208 ms
199,168 KB
testcase_43 AC 214 ms
199,040 KB
testcase_44 AC 216 ms
199,040 KB
testcase_45 AC 214 ms
199,168 KB
testcase_46 AC 201 ms
199,168 KB
testcase_47 AC 206 ms
199,040 KB
testcase_48 AC 206 ms
199,040 KB
testcase_49 AC 208 ms
199,168 KB
testcase_50 AC 204 ms
199,168 KB
testcase_51 AC 204 ms
199,040 KB
testcase_52 AC 183 ms
180,480 KB
testcase_53 AC 211 ms
199,040 KB
testcase_54 AC 200 ms
199,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

constexpr int INF = 1001001001;
int dp[5005][5005][2];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<pair<int, int>> dat;
    for(int i = 0; i < N; ++i){
        int P, D;
        cin >> P >> D;
        dat.emplace_back(P, D);
    }
    sort(rbegin(dat), rend(dat));

    for(int i = 0; i <= N; ++i){
        for(int s = 0; s <= K; ++s){
            for(int f = 0; f < 2; ++f){
                dp[i][s][f] = -INF;
            }
        }
    }

    dp[0][0][0] = 0;
    for(int i = 0; i < N; ++i){
        auto [P, D] = dat[i];
        for(int s = 0; s <= K; ++s){
            chmax(dp[i + 1][s][0], dp[i][s][0]);
            chmax(dp[i + 1][s][1], dp[i][s][1]);
            chmax(dp[i + 1][s][0], dp[i][s][1] + D);
            if(s + P <= K){
                chmax(dp[i + 1][s + P][1], max(dp[i][s][0], dp[i][s][1]) + D);
            }
        }
    }

    int ans = -INF;
    for(int s = 0; s <= K; ++s){
        for(int f = 0; f < 2; ++f){
            chmax(ans, dp[N][s][f]);
        }
    }

    cout << ans << endl;

    return 0;
}
0