結果

問題 No.1861 Required Number
ユーザー umezo
提出日時 2022-03-04 22:41:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 135 ms / 2,500 ms
コード長 1,195 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 197,060 KB
最終ジャッジ日時 2025-01-28 05:51:47
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42 RE * 4
権限があれば一括ダウンロードができます

ソースコード

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