結果

問題 No.974 最後の日までに
ユーザー drken1215
提出日時 2020-01-19 10:00:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 864 ms / 2,000 ms
コード長 1,613 bytes
コンパイル時間 995 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2024-10-04 01:41:11
合計ジャッジ時間 19,110 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using pll = pair<long long, long long>;
const long long INF = 1LL<<60;
int N;
vector<long long> a, b, c;

// ma[所持金 down 分] = 好感度 up 分
void rec(int n, int finish, map<long long, long long> &ma, long long syoji = 0, long long love = 0) {
    if (n == finish) {
        chmax(ma[-syoji], love);
        return;
    }

    // アルバイト
    rec(n + 1, finish, ma, syoji + a[n], love);

    // 学校からのデート
    if (n + 2 <= finish) rec(n + 2, finish, ma, syoji - c[n+1], love + b[n+1]);
}

long long solve(int mid) {
    // 前半と後半
    map<long long, long long> former, latter;
    rec(0, mid, former);
    rec(mid, N, latter);

    // 後半の累積 max
    long long cur = -INF;
    map<long long, long long> maxlatter;
    maxlatter[-INF] = -INF;
    for (auto it : latter) {
        maxlatter[it.first] = max(cur, it.second);
        chmax(cur, it.second);
    }

    // まとめ
    long long res = 0;
    for (auto it : former) {
        long long need = -(it.first);
        auto it2 = maxlatter.upper_bound(need);
        --it2;
        chmax(res, it.second + it2->second);
    }
    return res;
}

int main() {
    cin >> N;
    a.resize(N), b.resize(N), c.resize(N);
    for (int i = 0; i < N; ++i) cin >> a[i] >> b[i] >> c[i];
    cout << max(solve(N/2), solve(N/2+1)) << endl;
}
0