結果
| 問題 |
No.974 最後の日までに
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-25 13:01:11 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 911 ms / 2,000 ms |
| コード長 | 1,779 bytes |
| コンパイル時間 | 1,501 ms |
| コンパイル使用メモリ | 86,380 KB |
| 実行使用メモリ | 31,104 KB |
| 最終ジャッジ日時 | 2024-09-13 11:00:54 |
| 合計ジャッジ時間 | 20,822 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 49 |
ソースコード
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using ll = long long;
const ll INF = 52000000000000;
std::vector<ll> a, b, c;
void DFS(int step, int due, std::map<ll, ll> &loveBySpentMoney, ll spentMoney, ll love)
{
if (step == due)
{
// ma[足りない所持金] = 好感度
loveBySpentMoney[spentMoney] = std::max(loveBySpentMoney[spentMoney], love);
return;
}
// アルバイト
DFS(step + 1, due, loveBySpentMoney, spentMoney - a[step], love);
// 学校行って、次のステップでデート
if (step + 2 <= due)
DFS(step + 2, due, loveBySpentMoney, spentMoney + c[step + 1], love + b[step + 1]);
}
ll solve(int mid, int due)
{
// 前半と後半で分けて考える
std::map<ll, ll> prevs, posts;
DFS(0, mid, prevs, 0, 0);
DFS(mid, due, posts, 0, 0);
// 後半で所持金の状態ごとの好感度の最大値を出しておく.
auto cur = -INF;
posts[-INF] = -INF;
// map なのでキー順(=消費金額順)にソートされているはず.
for (auto it : posts)
{
posts[it.first] = std::max(cur, it.second);
cur = std::max(cur, it.second);
}
// 前半は後半で足りなくなる分の資金を先に稼いでおいたものを採用
ll res = 0;
for (auto prev : prevs)
{
auto need = prev.first;
auto post = posts.upper_bound(-need);
--post;
res = std::max(res, prev.second + post->second);
}
return res;
}
int main()
{
int N;
std::cin >> N;
a.resize(N), b.resize(N), c.resize(N);
for (auto i = 0; i < N; ++i)
std::cin >> a[i] >> b[i] >> c[i];
std::cout << std::max(solve(N / 2, N), solve(N / 2 + 1, N)) << std::endl;
}