結果

問題 No.1861 Required Number
ユーザー umezoumezo
提出日時 2022-03-04 22:41:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 122 ms / 2,500 ms
コード長 1,195 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 202,196 KB
実行使用メモリ 82,552 KB
最終ジャッジ日時 2023-09-26 03:46:15
合計ジャッジ時間 10,807 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,656 KB
testcase_01 AC 2 ms
5,436 KB
testcase_02 AC 2 ms
5,448 KB
testcase_03 AC 122 ms
82,364 KB
testcase_04 AC 122 ms
82,408 KB
testcase_05 AC 2 ms
5,644 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
5,540 KB
testcase_08 AC 3 ms
6,108 KB
testcase_09 AC 2 ms
5,460 KB
testcase_10 AC 2 ms
5,620 KB
testcase_11 AC 2 ms
5,636 KB
testcase_12 AC 2 ms
5,504 KB
testcase_13 AC 2 ms
5,784 KB
testcase_14 AC 2 ms
5,576 KB
testcase_15 AC 3 ms
6,040 KB
testcase_16 AC 2 ms
5,484 KB
testcase_17 AC 2 ms
5,528 KB
testcase_18 AC 2 ms
5,644 KB
testcase_19 AC 3 ms
5,824 KB
testcase_20 AC 3 ms
5,840 KB
testcase_21 AC 3 ms
5,696 KB
testcase_22 AC 3 ms
5,900 KB
testcase_23 AC 2 ms
5,736 KB
testcase_24 AC 53 ms
40,048 KB
testcase_25 AC 117 ms
79,952 KB
testcase_26 AC 55 ms
48,148 KB
testcase_27 AC 86 ms
72,292 KB
testcase_28 AC 74 ms
60,016 KB
testcase_29 AC 88 ms
76,780 KB
testcase_30 AC 84 ms
80,256 KB
testcase_31 AC 112 ms
76,944 KB
testcase_32 AC 82 ms
77,120 KB
testcase_33 AC 103 ms
76,916 KB
testcase_34 AC 106 ms
76,640 KB
testcase_35 AC 115 ms
81,872 KB
testcase_36 AC 88 ms
77,028 KB
testcase_37 AC 98 ms
81,860 KB
testcase_38 AC 107 ms
76,704 KB
testcase_39 AC 96 ms
76,464 KB
testcase_40 AC 121 ms
82,552 KB
testcase_41 AC 114 ms
80,064 KB
testcase_42 AC 114 ms
82,396 KB
testcase_43 AC 97 ms
80,624 KB
testcase_44 AC 2 ms
5,480 KB
04_evil_1.txt RE -
04_evil_2.txt RE -
04_evil_3.txt RE -
04_evil_4.txt RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

int dp1[10010][1010],dp2[10010][1010];

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int n,k;
  cin>>n>>k;
  vector<int> A(n+1);
  rep(i,n) cin>>A[i+1];
  
  if(k==0){
    int cnt=0;
    rep(i,n){
      if(A[i+1]==0) cnt++;
    }
    if(cnt>1) cout<<0<<endl;
    else if(cnt==1) cout<<1<<endl;
    else cout<<-1<<endl;
    return 0;
  }
  
  dp1[0][0]=1;
  for(int i=1;i<=n;i++){
    for(int j=0;j<=k;j++){
      dp1[i][j]|=dp1[i-1][j];
      if(j-A[i]>=0) dp1[i][j]|=dp1[i-1][j-A[i]];
    }
  }
  dp2[n+1][0]=1;
  for(int i=n;i>=1;i--){
    for(int j=0;j<=k;j++){
      dp2[i][j]|=dp2[i+1][j];
      if(j-A[i]>=0) dp2[i][j]|=dp2[i+1][j-A[i]];
    }
  }
  
  int cnt=0;
  for(int i=1;i<=n;i++){
    bool b=true;
    for(int j=0;j<=k;j++){
      if(dp1[i-1][j] && dp2[i+1][k-j]) b=false;
    }
    if(b==false) continue;
    b=false;
    for(int j=0;j<=k-A[i];j++){
      if(dp1[i-1][j] && dp2[i+1][k-A[i]-j]) b=true;
    }
    if(b) cnt++;
  }
  if(dp1[n][k]==0) cout<<-1<<endl;
  else cout<<cnt<<endl;
  
  return 0;
}
0