結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 8 ms
9,344 KB
testcase_10 AC 4 ms
6,528 KB
testcase_11 AC 6 ms
7,424 KB
testcase_12 AC 53 ms
46,208 KB
testcase_13 AC 181 ms
158,336 KB
testcase_14 AC 32 ms
28,160 KB
testcase_15 AC 32 ms
31,104 KB
testcase_16 AC 51 ms
46,848 KB
testcase_17 AC 67 ms
65,536 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 5 ms
6,272 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 225 ms
199,040 KB
testcase_25 AC 232 ms
199,040 KB
testcase_26 AC 234 ms
199,040 KB
testcase_27 AC 239 ms
199,040 KB
testcase_28 AC 231 ms
199,168 KB
testcase_29 AC 229 ms
199,168 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 4 ms
5,504 KB
testcase_34 AC 6 ms
7,424 KB
testcase_35 AC 232 ms
199,040 KB
testcase_36 AC 233 ms
199,040 KB
testcase_37 AC 240 ms
199,168 KB
testcase_38 AC 232 ms
199,040 KB
testcase_39 AC 232 ms
199,040 KB
testcase_40 AC 229 ms
199,040 KB
testcase_41 AC 265 ms
199,040 KB
testcase_42 AC 237 ms
199,040 KB
testcase_43 AC 235 ms
199,040 KB
testcase_44 AC 235 ms
199,168 KB
testcase_45 AC 239 ms
199,168 KB
testcase_46 AC 234 ms
199,040 KB
testcase_47 AC 233 ms
199,168 KB
testcase_48 AC 231 ms
199,040 KB
testcase_49 AC 233 ms
199,040 KB
testcase_50 AC 230 ms
199,040 KB
testcase_51 AC 230 ms
199,040 KB
testcase_52 AC 195 ms
180,480 KB
testcase_53 AC 224 ms
199,168 KB
testcase_54 AC 230 ms
199,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

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

int dp[5005][5005][2], cum_max[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;
            }
        }
    }
    for(int s = 0; s <= K; ++s){
        for(int f = 0; f < 2; ++f){
            cum_max[s][f] = -INF;
        }
    }
    
    dp[0][0][0] = 0;
    for(int i = 0; i < N; ++i){
        for(int s = 0; s <= K; ++s){
            for(int f = 0; f < 2; ++f){
                chmax(cum_max[s][f], dp[i][s][f]);
                if(s)   chmax(cum_max[s][f], cum_max[s - 1][f]);
            }
        }

        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], cum_max[s][1] + D);
            if(s + P <= K){
                chmax(dp[i + 1][s + P][1], max(cum_max[s][0], cum_max[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