結果
問題 |
No.974 最後の日までに
|
ユーザー |
|
提出日時 | 2020-01-18 07:14:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 126 ms / 2,000 ms |
コード長 | 2,057 bytes |
コンパイル時間 | 2,255 ms |
コンパイル使用メモリ | 207,212 KB |
最終ジャッジ日時 | 2025-01-08 19:55:21 |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 49 |
ソースコード
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) using namespace std; template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); } int64_t solve(int w, const vector<int64_t> & a, const vector<int64_t> & b, const vector<int64_t> & c) { auto func = [&](int l, int r) { vector<pair<int64_t, int64_t> > nxt, cur, prv; cur.emplace_back(0, 0); REP3 (i, l, r) { prv.swap(cur); cur.swap(nxt); nxt.clear(); for (auto it : prv) { cur.emplace_back(it.first + a[i], it.second); if (i + 1 < r) { nxt.emplace_back(it.first - c[i + 1], it.second + b[i + 1]); } } } sort(ALL(cur)); nxt.clear(); for (auto it : cur) { while (not nxt.empty() and nxt.back().second <= it.second) { nxt.pop_back(); } nxt.push_back(it); } return nxt; }; auto merge = [&](const vector<pair<int64_t, int64_t> > & dp1, const vector<pair<int64_t, int64_t> > & dp2) { int64_t ans = INT64_MIN; for (auto it1 : dp1) { auto it2 = lower_bound(ALL(dp2), make_pair(- it1.first, INT64_MIN)); if (it2 != dp2.end()) { chmax(ans, it1.second + it2->second); } } return ans; }; int hw = w / 2; auto l1 = func(0, hw); auto r1 = func(hw, w); auto l2 = func(0, hw + 1); auto r2 = func(hw + 1, w); return max(merge(l1, r1), merge(l2, r2)); } int main() { int w; cin >> w; vector<int64_t> a(w), b(w), c(w); REP (i, w) { cin >> a[i] >> b[i] >> c[i]; } cout << solve(w, a, b, c) << endl; return 0; }