結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー outlineoutline
提出日時 2021-01-14 04:30:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 452 ms / 2,000 ms
コード長 2,460 bytes
コンパイル時間 1,486 ms
コンパイル使用メモリ 144,368 KB
実行使用メモリ 394,752 KB
最終ジャッジ日時 2024-05-02 20:43:15
合計ジャッジ時間 15,679 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 14 ms
15,104 KB
testcase_10 AC 8 ms
9,728 KB
testcase_11 AC 10 ms
11,520 KB
testcase_12 AC 96 ms
89,088 KB
testcase_13 AC 336 ms
312,832 KB
testcase_14 AC 58 ms
53,120 KB
testcase_15 AC 61 ms
58,752 KB
testcase_16 AC 94 ms
90,112 KB
testcase_17 AC 129 ms
127,232 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 8 ms
9,088 KB
testcase_22 AC 5 ms
5,888 KB
testcase_23 AC 6 ms
7,168 KB
testcase_24 AC 444 ms
394,624 KB
testcase_25 AC 442 ms
394,624 KB
testcase_26 AC 437 ms
394,496 KB
testcase_27 AC 441 ms
394,624 KB
testcase_28 AC 438 ms
394,624 KB
testcase_29 AC 439 ms
394,496 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 6 ms
6,400 KB
testcase_33 AC 7 ms
7,680 KB
testcase_34 AC 12 ms
11,264 KB
testcase_35 AC 441 ms
394,624 KB
testcase_36 AC 441 ms
394,624 KB
testcase_37 AC 433 ms
394,624 KB
testcase_38 AC 438 ms
394,624 KB
testcase_39 AC 436 ms
394,624 KB
testcase_40 AC 437 ms
394,624 KB
testcase_41 AC 444 ms
394,496 KB
testcase_42 AC 441 ms
394,624 KB
testcase_43 AC 440 ms
394,624 KB
testcase_44 AC 438 ms
394,624 KB
testcase_45 AC 439 ms
394,624 KB
testcase_46 AC 451 ms
394,624 KB
testcase_47 AC 443 ms
394,624 KB
testcase_48 AC 440 ms
394,624 KB
testcase_49 AC 442 ms
394,496 KB
testcase_50 AC 440 ms
394,752 KB
testcase_51 AC 438 ms
394,752 KB
testcase_52 AC 386 ms
357,504 KB
testcase_53 AC 445 ms
394,752 KB
testcase_54 AC 452 ms
394,496 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][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] = cum_max[i][s][f] = -INF;
            }
        }
    }
    
    dp[0][0][0] = 0;
    for(int i = 0; i < N; ++i){
        cum_max[i][0][0] = dp[i][0][0];
        cum_max[i][0][1] = dp[i][0][1];
        for(int s = 0; s < K; ++s){
            for(int f = 0; f < 2; ++f){
                chmax(cum_max[i][s + 1][f], max(cum_max[i][s][f], dp[i][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[i][s][1] + D);
            if(s + P <= K){
                chmax(dp[i + 1][s + P][1], max(cum_max[i][s][0], cum_max[i][s][1]) + D);
            }
        }

        for(int s = 0; s <= K; ++s){
            for(int f = 0; f < 2; ++f){
                cum_max[i + 1][s][f] = cum_max[i][s][f];
            }
        }
    }

    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