結果

問題 No.974 最後の日までに
ユーザー kyo1kyo1
提出日時 2020-06-16 13:47:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,519 bytes
コンパイル時間 2,648 ms
コンパイル使用メモリ 208,112 KB
実行使用メモリ 15,328 KB
最終ジャッジ日時 2023-09-16 10:58:27
合計ジャッジ時間 9,069 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
15,248 KB
testcase_01 AC 116 ms
13,628 KB
testcase_02 AC 114 ms
13,980 KB
testcase_03 AC 116 ms
13,792 KB
testcase_04 AC 118 ms
15,328 KB
testcase_05 AC 115 ms
13,916 KB
testcase_06 AC 115 ms
14,120 KB
testcase_07 AC 117 ms
13,720 KB
testcase_08 AC 116 ms
13,936 KB
testcase_09 AC 117 ms
13,688 KB
testcase_10 AC 119 ms
14,352 KB
testcase_11 AC 119 ms
13,804 KB
testcase_12 AC 117 ms
13,628 KB
testcase_13 AC 120 ms
15,268 KB
testcase_14 AC 119 ms
13,684 KB
testcase_15 AC 118 ms
13,624 KB
testcase_16 AC 117 ms
13,624 KB
testcase_17 AC 136 ms
13,628 KB
testcase_18 AC 135 ms
13,832 KB
testcase_19 AC 134 ms
14,564 KB
testcase_20 AC 136 ms
14,824 KB
testcase_21 AC 133 ms
13,932 KB
testcase_22 AC 136 ms
13,908 KB
testcase_23 AC 134 ms
13,684 KB
testcase_24 AC 135 ms
13,972 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,504 KB
testcase_27 AC 93 ms
12,468 KB
testcase_28 AC 120 ms
13,696 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 137 ms
13,628 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 77 ms
9,500 KB
testcase_34 AC 125 ms
13,628 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 136 ms
14,276 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 82 ms
12,264 KB
testcase_41 AC 108 ms
13,624 KB
testcase_42 AC 135 ms
13,636 KB
testcase_43 AC 134 ms
13,928 KB
testcase_44 AC 102 ms
12,240 KB
testcase_45 AC 84 ms
9,280 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 1 ms
4,376 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int W;
  cin >> W;
  vector<int64_t> A(W), B(W), C(W);
  for (int i = 0; i < W; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }
  function<void(int, int, int64_t, int64_t, vector<pair<int64_t, int64_t>> &)> dfs =
      [&](int now, int end, int64_t money, int64_t love, vector<pair<int64_t, int64_t>> &vec) {
        if (now == end) {
          vec.emplace_back(make_pair(money, love));
          return;
        }
        dfs(now + 1, end, money + A[now], love, vec);
        if (now + 1 < end) dfs(now + 2, end, money - C[now + 1], love + B[now + 1], vec);
      };
  auto solve = [&](int w) {
    vector<pair<int64_t, int64_t>> A, B;
    dfs(0, w, 0, 0, A);
    dfs(w, W, 0, 0, B);
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    for (int i = (int)A.size() - 2; i > -1; i--) {
      chmax(A[i].second, A[i + 1].second);
    }
    for (int i = (int)B.size() - 2; i > -1; i--) {
      chmax(B[i].second, B[i + 1].second);
    }
    int64_t res = 0;
    for (int i = 0; i < (int)A.size(); i++) {
      auto itr = lower_bound(B.begin(), B.end(), make_pair(-A[i].first, -INF));
      if (itr != B.end()) res = max(res, itr->second + A[i].second);
    }
    return res;
  };
  cout << max(solve(W / 2), solve(W / 2 + 1)) << '\n';
  return 0;
}
0