結果
| 問題 | No.1045 直方体大学 | 
| コンテスト | |
| ユーザー |  minato | 
| 提出日時 | 2020-05-01 23:10:21 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 196 ms / 2,000 ms | 
| コード長 | 3,279 bytes | 
| コンパイル時間 | 2,234 ms | 
| コンパイル使用メモリ | 182,556 KB | 
| 実行使用メモリ | 28,032 KB | 
| 最終ジャッジ日時 | 2024-12-25 14:14:40 | 
| 合計ジャッジ時間 | 3,806 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 17 | 
ソースコード
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long; 
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll dp[1<<16][16][3]; //A*B 1, B*C 2, C*A 3
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<ll> A(N),B(N),C(N);
    rep(i,N) cin >> A[i] >> B[i] >> C[i];
    rep(mask,1<<N) rep(i,N) rep(j,3) dp[mask][i][j] = -1e18;
    dp[0][0][0] = 0;
    auto check=[](ll a, ll b, ll c, ll d) {
        if (a < b) swap(a,b);
        if (c < d) swap(c,d);
        return a <= c and b <= d;
    };
    rep(i,N) {
        dp[1<<i][i][0] = C[i];
        dp[1<<i][i][1] = A[i];
        dp[1<<i][i][2] = B[i];
    }
    for (int mask = 1; mask < (1<<N); ++mask) {
        rep(i,N) {
            if (mask&(1<<i)) {
                rep(j,N) {
                    if (mask&(1<<j)) continue;
                    if (dp[mask][i][0] != -1e18) {
                        if (check(A[j],B[j],A[i],B[i])) {
                            chmax(dp[mask+(1<<j)][j][0],dp[mask][i][0]+C[j]);
                        }
                        if (check(B[j],C[j],A[i],B[i])) {
                            chmax(dp[mask+(1<<j)][j][1],dp[mask][i][0]+A[j]);
                        }
                        if (check(C[j],A[j],A[i],B[i])) {
                            chmax(dp[mask+(1<<j)][j][2],dp[mask][i][0]+B[j]);
                        }
                    }
                    if (dp[mask][i][1] != -1e18) {
                        if (check(A[j],B[j],B[i],C[i])) {
                            chmax(dp[mask+(1<<j)][j][0],dp[mask][i][1]+C[j]);
                        }
                        if (check(B[j],C[j],B[i],C[i])) {
                            chmax(dp[mask+(1<<j)][j][1],dp[mask][i][1]+A[j]);
                        }
                        if (check(C[j],A[j],B[i],C[i])) {
                            chmax(dp[mask+(1<<j)][j][2],dp[mask][i][1]+B[j]);
                        }
                    }
                    if (dp[mask][i][2] != -1e18) {
                        if (check(A[j],B[j],C[i],A[i])) {
                            chmax(dp[mask+(1<<j)][j][0],dp[mask][i][2]+C[j]);
                        }
                        if (check(B[j],C[j],C[i],A[i])) {
                            chmax(dp[mask+(1<<j)][j][1],dp[mask][i][2]+A[j]);
                        }
                        if (check(C[j],A[j],C[i],A[i])) {
                            chmax(dp[mask+(1<<j)][j][2],dp[mask][i][2]+B[j]);
                        }
                    }
                }
            }
        }
    }
    ll ans = 0;
    rep(mask,1<<N) rep(i,N) rep(j,3) chmax(ans,dp[mask][i][j]);
    cout << ans << ln;
}
            
            
            
        