結果

問題 No.437 cwwゲーム
コンテスト
ユーザー a01sa01to
提出日時 2025-11-01 00:20:09
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,040 bytes
コンパイル時間 3,862 ms
コンパイル使用メモリ 288,500 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-11-01 00:20:15
合計ジャッジ時間 5,157 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  string a;
  cin >> a;
  const int n = a.size();
  vector<tuple<int, int, int>> cand;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      for (int k = j + 1; k < n; ++k) {
        if (a[i] != '0' && a[i] != a[j] && a[j] == a[k]) cand.push_back({ i, j, k });
      }
    }
  }
  vector<ll> dp(1 << n, -1e18);
  queue<int> q;
  dp[0] = 0;
  q.push(0);
  while (!q.empty()) {
    int bit = q.front();
    q.pop();
    for (auto [i, j, k] : cand) {
      if (bit & ((1 << i) | (1 << j) | (1 << k))) continue;
      int nbit = bit | (1 << i) | (1 << j) | (1 << k);
      ll nscore = dp[bit] + (a[i] - '0') * 100 + (a[j] - '0') * 10 + (a[k] - '0');
      if (dp[nbit] < nscore) {
        dp[nbit] = nscore;
        q.push(nbit);
      }
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';
  return 0;
}
0