結果

問題 No.818 Dinner time
ユーザー ninhtay
提出日時 2025-06-22 00:04:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,110 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 81,800 KB
実行使用メモリ 814,464 KB
最終ジャッジ日時 2025-06-22 00:04:39
合計ジャッジ時間 3,741 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 2
other AC * 2 WA * 11 MLE * 1 -- * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;
const ll NEG_INF = -1e18;

int main() {
    int N, M;
    cin >> N >> M;
    vector<ll> A(N), B(N);
    for (int i = 0; i < N; ++i) cin >> A[i]>>B[i];

    vector<vector<ll>> dp(N + 1, vector<ll>(M + 1, NEG_INF));
    dp[0][M] = 0; 

    for (int i = 0; i < N; ++i) {
        
        vector<ll> max_suffix(M + 2, NEG_INF); 
        max_suffix[M] = dp[i][M];
        for (int j = M - 1; j >= 0; --j)
            max_suffix[j] = max(dp[i][j], max_suffix[j + 1]);

        
        for (int j = 0; j <= M; ++j) {
            ll best_before = max_suffix[j]; 
            if (best_before == NEG_INF) continue;

            ll egg = j * A[i];
            ll meat = B[i];
            ll egg_then_meat = (j > 0) ? (j - 1) * A[i] + B[i] : B[i];
            ll gain = max({egg, meat, egg_then_meat});

            dp[i + 1][j] = max(dp[i + 1][j], best_before + gain);
        }
    }

    ll res = NEG_INF;
    for (int j = 0; j <= M; ++j)
        res = max(res, dp[N][j]);

    cout << res << endl;
    return 0;
}
0