結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー hitonanode
提出日時 2019-12-14 00:14:28
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,145 bytes
コンパイル時間 1,802 ms
コンパイル使用メモリ 184,604 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 23:09:25
合計ジャッジ時間 6,291 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }


int main()
{
    int N, K;
    cin >> N >> K;
    vector<pint> PD(N);
    REP(i, N) cin >> PD[i].first >> PD[i].second;
    sort(PD.rbegin(), PD.rend());

    vector<vector<int>> dp(2, vector<int>(K + 1));
    REP(j, K + 1) REP(k, 2) dp[k][j] = -1e9;
    dp[0][K] = 0;

    for (auto p : PD)
    {
        vector<vector<int>> dpnew = dp;
        REP(d, 2) REP(j, K + 1)
        {
            if (dp[d][j] >= 0)
            {
                if (j - p.first >= 0) mmax(dpnew[1][j - p.first], dp[d][j] + p.second);
            }
        }
        REP(j, K + 1) if (dp[1][j] >= 0) mmax(dpnew[0][j], dp[1][j] + p.second);
        dp = dpnew;
    }
    int ret = 0;
    REP(k, 2) REP(j, K + 1) mmax(ret, dp[k][j]);
    cout << ret << endl;
}
0