結果
| 問題 |
No.951 【本日限定】1枚頼むともう1枚無料!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-14 04:50:24 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 458 ms / 2,000 ms |
| コード長 | 1,312 bytes |
| コンパイル時間 | 1,368 ms |
| コンパイル使用メモリ | 82,580 KB |
| 最終ジャッジ日時 | 2025-01-17 17:40:41 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 52 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
constexpr int INF = 1001001001;
int dp[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] = -INF;
}
}
}
dp[0][0][0] = 0;
for(int i = 0; i < N; ++i){
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], dp[i][s][1] + D);
if(s + P <= K){
chmax(dp[i + 1][s + P][1], max(dp[i][s][0], dp[i][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;
}