結果

問題 No.974 最後の日までに
ユーザー shibh308shibh308
提出日時 2019-12-10 08:12:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,325 bytes
コンパイル時間 2,119 ms
コンパイル使用メモリ 205,488 KB
実行使用メモリ 7,608 KB
最終ジャッジ日時 2023-09-07 23:21:34
合計ジャッジ時間 6,033 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 228 ms
4,380 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

using i64 = long long;

const i64 MOD = 1000000007;

int n;
vector<i64> a, b, c, r, m;
i64 ans;

i64 greedy(int l){
    i64 money = 0, ret = 0;
    for(int i = l; i < n; ++i){
        if(i != n - 1 && money >= c[i + 1]){
            money -= c[i + 1];
            ret += b[i + 1];
            ++i;
        }
        else{
            money += a[i];
        }
    }
    for(int i = 0; i < l - 1; ++i){
        if(money >= c[i + 1]){
            money -= c[i + 1];
            ret += b[i + 1];
            ++i;
        }
        else{
            money += a[i];
        }
    }
    return ret;
}

i64 greedy_inv(int s){
    i64 money = 0, ret = 0;
    for(int i = s; i >= 0; --i){
        if(i && money >= c[i]){
            money -= c[i];
            money -= a[i];
            ret += b[i];
            --i;
        }
        else{
            money += a[i];
        }
    }
    for(int i = n - 1; i > s + 1; --i){
        if(i != s && money >= c[i]){
            money -= c[i];
            money -= a[i];
            ret += b[i];
            --i;
        }
        else{
            money += a[i];
        }
    }
    return ret;
}

// [i, n)
bool cond(int i, i64 money, i64 val){
    if(val + r[i] < ans)
        return false;
    if(money + m[i] < 0)
        return false;
    return true;
}

void dfs(int i, i64 money, i64 val){
    if(i == n){
        if(money >= 0)
            ans = max(ans, val);
        return;
    }
    if(!cond(i, money, val))
        return;
    if(i != n - 1)
        dfs(i + 2, money - c[i + 1], val + b[i + 1]);
    dfs(i + 1, money + a[i], val);
}

signed 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];
    a.emplace_back(0);
    b.emplace_back(0);
    c.emplace_back(0);
    a.emplace_back(0);
    b.emplace_back(0);
    c.emplace_back(0);
    r.resize(n + 1, 0);
    m.resize(n + 1, 0);
    for(int i = n - 2; i >= 0; --i)
        r[i] = max({r[i], r[i + 1], r[i + 2] + b[i + 1]});
    for(int i = n - 1; i >= 0; --i)
        m[i] = m[i + 1] + a[i];

    ans = 0;
    for(int i = 0; i < n; ++i)
        ans = max(ans, greedy(i));
    for(int i = 0; i < n; ++i)
        ans = max(ans, greedy_inv(i));

    dfs(0, 0, 0);

    cout << ans << endl;
}

0