提出 #12902620


ソースコード 拡げる

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i,a,b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i,a,b) for (ll i = (a); i <= (ll)(b); ++i)

void chmax(ll& a, ll b) { a = max(a, b); }
void chmin(ll& a, ll b) { a = min(a, b); }

using P = pair<int, int>;

int main() {
    int n,t; cin >> n >> t;
    vector<P> ba;
    rep(i, n) {
        int a,b;
        cin >> a >> b;
        ba.push_back(make_pair(b,a));
    }
    //sort(ba.rbegin(), ba.rend());
    vector<vector<int>> dp1(n+1, vector<int>(t, 0));
    vector<vector<int>> dp2(n+1, vector<int>(t, 0));

    vector<int> ans1(n,0);
    vector<int> ans2(n,0);
    rep(i, n) {
        rep(j, t) {
            if(j >= ba[i].second) {
                if(i > 0) dp1[i][j] = max(dp1[i-1][j-ba[i].second] + ba[i].first, dp1[i-1][j]);
                else dp1[i][j] = ba[i].first;
            } else {
                if(i > 0) dp1[i][j] = dp1[i-1][j];
            }
            ans1[i] = max(ans1[i], dp1[i][j]);
        }
    }

    for(int i=n-1; i >= 0; i--) {
        rep(j, t) {
            if(j >= ba[i].second) {
                dp2[i][j] = max(dp2[i+1][j-ba[i].second] + ba[i].first, dp2[i+1][j]);
            } else {
                dp2[i][j] = dp2[i+1][j];
            }
            ans2[i] = max(ans2[i], dp2[i][j]);
        }
    }

    int ans = 0;
    rep(i, n) {
        int tmpans = 0;
        rep(j, t) {
            if(i == 0) {
                tmpans = max(tmpans, dp2[i + 1][j]);
            } else if(i == n-1) {
                tmpans = max(tmpans, dp1[i - 1][j]);
            } else {
                tmpans = max(tmpans, dp1[i - 1][j] + dp2[i + 1][t - 1 - j]);
            }
        }
        ans = max(ans, tmpans+ba[i].first);
    }

    cout << ans << endl;
    return 0;
}


/**
3 1000
2 40
500 20
500 12


5 900
200 1
100 2
1000 3
900 4
900 5
**/

提出情報

提出日時
問題 E - All-you-can-eat
ユーザ esaka
言語 C++14 (GCC 5.4.1)
得点 500
コード長 2045 Byte
結果 AC
実行時間 102 ms
メモリ 70912 KiB

ジャッジ結果

セット名 Sample All after_contest
得点 / 配点 0 / 0 500 / 500 0 / 0
結果
AC × 4
AC × 31
AC × 1
セット名 テストケース
Sample sample_01, sample_02, sample_03, sample_04
All corner_01, corner_02, corner_03, corner_04, corner_05, corner_06, corner_07, hand_01, hand_02, max_01, max_02, max_03, max_04, max_05, max_06, max_07, max_08, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, sample_01, sample_02, sample_03, sample_04
after_contest after_contest_01
ケース名 結果 実行時間 メモリ
after_contest_01 AC 1 ms 256 KiB
corner_01 AC 6 ms 3456 KiB
corner_02 AC 23 ms 15360 KiB
corner_03 AC 24 ms 17024 KiB
corner_04 AC 5 ms 3072 KiB
corner_05 AC 5 ms 3328 KiB
corner_06 AC 5 ms 3200 KiB
corner_07 AC 22 ms 16000 KiB
hand_01 AC 1 ms 256 KiB
hand_02 AC 1 ms 256 KiB
max_01 AC 97 ms 69632 KiB
max_02 AC 94 ms 67584 KiB
max_03 AC 102 ms 70912 KiB
max_04 AC 102 ms 70912 KiB
max_05 AC 96 ms 70912 KiB
max_06 AC 96 ms 70912 KiB
max_07 AC 96 ms 70912 KiB
max_08 AC 96 ms 70912 KiB
random_01 AC 19 ms 11904 KiB
random_02 AC 10 ms 6272 KiB
random_03 AC 35 ms 24576 KiB
random_04 AC 10 ms 6144 KiB
random_05 AC 16 ms 10624 KiB
random_06 AC 49 ms 34048 KiB
random_07 AC 48 ms 33152 KiB
random_08 AC 9 ms 6016 KiB
random_09 AC 37 ms 25472 KiB
random_10 AC 40 ms 27904 KiB
sample_01 AC 1 ms 256 KiB
sample_02 AC 1 ms 256 KiB
sample_03 AC 1 ms 256 KiB
sample_04 AC 1 ms 256 KiB