結果

問題 No.10 +か×か
ユーザー codershifthcodershifth
提出日時 2016-08-30 01:20:19
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,018 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 167,048 KB
実行使用メモリ 814,408 KB
最終ジャッジ日時 2023-08-09 01:46:28
合計ジャッジ時間 8,726 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 MLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class Yuki0010
{
public:
    void solve(void)
    {
            int N,Total;
            cin>>N>>Total;

            vector<int> A(N);
            REP(i,N) cin>>A[i];

            vector<vector<bool>> dp(N+1, vector<bool>(Total+1, false));
            dp[0][A[0]] = true;

            REP(i,N-1)
            {
                REP(s,Total+1)
                {
                    if ( !dp[i][s] )
                        continue;
                    int a = A[i+1];
                    if ( s+a <= Total )
                        dp[i+1][s+a] = true;
                    if ( s*a <= Total )
                        dp[i+1][s*a] = true;
                }
            }
            vector<string> ans;
            set<string> vis;
            function<void(int,int,string)> collect = [&](int s, int k, string str)
            {
                if ( vis.count(str) )
                    return;
                vis.insert(str);
                if ( s > Total )
                    return;
                if ( k > N-1 )
                    return;
                if ( !dp[k][s] )
                    return;
                if ( k == N-1 )
                {
                    if ( s == Total )
                    {
                        ans.push_back(str);
                        return;
                    }
                    return;
                }
                collect(s+A[k+1], k+1, str+'+');
                collect(s*A[k+1], k+1, str+'*');
            };
            collect(A[0], 0, "");
            sort(RANGE(ans));
            reverse(RANGE(ans));
            cout<<ans[0]<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new Yuki0010();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0