結果

問題 No.10 +か×か
ユーザー monnumonnu
提出日時 2021-07-12 00:41:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 5,000 ms
コード長 904 bytes
コンパイル時間 1,628 ms
コンパイル使用メモリ 171,780 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 06:33:36
合計ジャッジ時間 2,462 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll=long long;
using Graph=vector<vector<int>>;
#define MAX 1000000
//#define MOD 1000000007
#define MOD 998244353
//#define INF 1000000000
#define INF 1000000000000000000

int main(){
  int N;
  int S;
  cin>>N>>S;
  vector<int> A(N);
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  vector<vector<bool>> dp(N,vector<bool>(S+1,false));
  dp[N-1][S]=true;
  for(int i=N-2;i>=0;i--){
    for(int j=0;j<=S;j++){
      if(j+A[i+1]<=S){
        dp[i][j]=dp[i][j]|dp[i+1][j+A[i+1]];
      }
      if(j*A[i+1]<=S){
        dp[i][j]=dp[i][j]|dp[i+1][j*A[i+1]];
      }
    }
  }
  assert(dp[0][A[0]]);
  int j=A[0];
  //cout<<"hello"<<endl;
  for(int i=0;i<N-1;i++){
    if(j+A[i+1]<=S&&dp[i+1][j+A[i+1]]){
      j+=A[i+1];
      cout<<'+';
    }else{
      j*=A[i+1];
      cout<<'*';
    }
  }
  cout<<endl;
}
0