結果

問題 No.1872 Dictionary Order
ユーザー SSRS
提出日時 2022-03-11 21:43:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,193 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 202,640 KB
最終ジャッジ日時 2025-01-28 08:26:43
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<int> P(N);
  for (int i = 0; i < N; i++){
    cin >> P[i];
    P[i]--;
  }
  vector<vector<bool>> dp(N + 1, vector<bool>(M + 1, false));
  dp[N][0] = true;
  for (int i = N - 1; i >= 0; i--){
    for (int j = 0; j <= M; j++){
      if (dp[i + 1][j]){
        dp[i][j] = true;
        if (j + A[i] <= M){
          dp[i][j + A[i]] = true;
        }
      }
    }
  }
  if (!dp[0][M]){
    cout << -1 << endl;
  } else {
    vector<int> Q(N);
    for (int i = 0; i < N; i++){
      Q[P[i]] = i;
    }
    vector<int> ans;
    int sum = 0;
    int L = 0;
    while (sum < M){
      for (int i = 0; i < N; i++){
        if (Q[i] > L && sum + A[Q[i]] <= M){
          if (dp[Q[i] + 1][M - A[Q[i]] - sum]){
            ans.push_back(Q[i]);
            L = Q[i];
            sum += A[Q[i]];
            break;
          }
        }
      }
    }
    int K = ans.size();
    cout << K << endl;
    for (int i = 0; i < K; i++){
      cout << ans[i] + 1;
      if (i < K - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}
0