結果

問題 No.974 最後の日までに
ユーザー 0w10w1
提出日時 2020-01-23 20:08:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 3,063 bytes
コンパイル時間 2,682 ms
コンパイル使用メモリ 218,260 KB
実行使用メモリ 40,532 KB
最終ジャッジ日時 2024-04-14 22:00:36
合計ジャッジ時間 8,811 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
39,548 KB
testcase_01 AC 133 ms
40,104 KB
testcase_02 AC 124 ms
39,684 KB
testcase_03 AC 125 ms
39,320 KB
testcase_04 AC 131 ms
40,408 KB
testcase_05 AC 136 ms
39,216 KB
testcase_06 AC 136 ms
38,996 KB
testcase_07 AC 126 ms
39,492 KB
testcase_08 AC 123 ms
38,452 KB
testcase_09 AC 130 ms
39,204 KB
testcase_10 AC 128 ms
39,352 KB
testcase_11 AC 123 ms
38,508 KB
testcase_12 AC 121 ms
38,620 KB
testcase_13 AC 127 ms
39,016 KB
testcase_14 AC 129 ms
39,520 KB
testcase_15 AC 124 ms
40,532 KB
testcase_16 AC 124 ms
39,392 KB
testcase_17 AC 111 ms
38,460 KB
testcase_18 AC 109 ms
39,488 KB
testcase_19 AC 109 ms
38,624 KB
testcase_20 AC 108 ms
40,288 KB
testcase_21 AC 110 ms
39,480 KB
testcase_22 AC 112 ms
39,796 KB
testcase_23 AC 109 ms
38,800 KB
testcase_24 AC 112 ms
39,412 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 126 ms
33,120 KB
testcase_28 AC 143 ms
39,148 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 136 ms
39,416 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 90 ms
24,900 KB
testcase_34 AC 162 ms
40,104 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 151 ms
39,064 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 83 ms
32,768 KB
testcase_41 AC 102 ms
39,756 KB
testcase_42 AC 152 ms
40,184 KB
testcase_43 AC 159 ms
39,088 KB
testcase_44 AC 130 ms
32,312 KB
testcase_45 AC 90 ms
24,604 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 2 ms
6,944 KB
testcase_49 AC 2 ms
6,944 KB
testcase_50 AC 2 ms
6,940 KB
testcase_51 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);

  int W;
  cin >> W;

  vector<int64_t> A(W);
  vector<int64_t> B(W);
  vector<int64_t> C(W);
  for (int i = 0; i < W; ++i) cin >> A[i] >> B[i] >> C[i];

  if (W == 1) {
    cout << 0 << endl;
    return 0;
  }

  function<void(int, int, int, int, int64_t, int64_t, map<int, vector<pair<int64_t, int64_t>>> &)>
      dfs = [&](int x, int f, int p, int g, int64_t love, int64_t money, map<int, vector<pair<int64_t, int64_t>>> &bag) {
    if (x == g) {
      bag[f << 2 | p].emplace_back(money, love);
      return;
    }
    if (f == -1) {
      dfs(x + 1, 0, 0, g, 0, 0, bag); // school
      dfs(x + 1, 1, 1, g, B[x], -C[x], bag); // date, check when combining
      dfs(x + 1, 2, 2, g, 0, A[x], bag); // work
    } else {
      if (p == 0) {
        dfs(x + 1, f, 1, g, love + B[x], money - C[x], bag);
      } else {
        dfs(x + 1, f, 0, g, love, money, bag);
        dfs(x + 1, f, 2, g, love, money + A[x], bag);
      }
    }
  };
  map<int, vector<pair<int64_t, int64_t>>> bag0;
  dfs(0, -1, -1, W / 2, 0, 0, bag0);
  map<int, vector<pair<int64_t, int64_t>>> bag1;
  dfs(W / 2, -1, -1, W, 0, 0, bag1);

  int64_t ans = 0; {
    vector<pair<int64_t, int64_t>> g1; {
      for (int i = 0; i < 3; ++i) if (i != 1) g1.insert(g1.end(), bag0[i << 2 | 0].begin(), bag0[i << 2 | 0].end());
      sort(g1.begin(), g1.end());
    }
    vector<pair<int64_t, int64_t>> d0; {
      for (int i = 0; i < 3; ++i) d0.insert(d0.end(), bag1[1 << 2 | i].begin(), bag1[1 << 2 | i].end());
    }
    vector<int64_t> pmaxloveg(g1.size());
    pmaxloveg.back() = g1.back().second;
    for (int i = g1.size() - 2; ~i; --i) {
      pmaxloveg[i] = max(pmaxloveg[i + 1], g1[i].second);
    }
    for (auto ml : d0) {
      int64_t money, love;
      tie(money, love) = ml;
      int x = lower_bound(g1.begin(), g1.end(), make_pair(-money, (int64_t) 0)) - g1.begin();
      if (x < g1.size()) ans = max(ans, love + pmaxloveg[x]);
    }
  } {
    vector<pair<int64_t, int64_t>> g1; {
      for (int i = 0; i < 3; ++i) if (i != 1) g1.insert(g1.end(), bag0[i << 2 | 1].begin(), bag0[i << 2 | 1].end());
      for (int i = 0; i < 3; ++i) if (i != 1) g1.insert(g1.end(), bag0[i << 2 | 2].begin(), bag0[i << 2 | 2].end());
      sort(g1.begin(), g1.end());
    }
    vector<pair<int64_t, int64_t>> d0; {
      for (int i = 0; i < 3; ++i) d0.insert(d0.end(), bag1[0 << 2 | i].begin(), bag1[0 << 2 | i].end());
      for (int i = 0; i < 3; ++i) d0.insert(d0.end(), bag1[2 << 2 | i].begin(), bag1[2 << 2 | i].end());
    }
    vector<int64_t> pmaxloveg(g1.size());
    pmaxloveg.back() = g1.back().second;
    for (int i = g1.size() - 2; ~i; --i) {
      pmaxloveg[i] = max(pmaxloveg[i + 1], g1[i].second);
    }
    for (auto ml : d0) {
      int64_t money, love;
      tie(money, love) = ml;
      int x = lower_bound(g1.begin(), g1.end(), make_pair(-money, (int64_t) 0)) - g1.begin();
      if (x < g1.size()) ans = max(ans, love + pmaxloveg[x]);
    }
  }

  cout << ans << endl;

  return 0;
}
0