結果

問題 No.914 Omiyage
ユーザー huka_huka
提出日時 2021-09-24 19:40:20
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 1,257 ms
コンパイル使用メモリ 164,308 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-05 09:46:32
合計ジャッジ時間 2,174 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

# ifndef ONLINE_JUDGE
# define _GLIBCXX_DEBUG
# endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
# define rep(i, n) for(int i = 0; i < (int)(n); i++)
# define all(v) v.begin(), v.end()

int main(){
  constexpr char endl = '\n';
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
  //input();
  int N, M, K;cin >> N >> M >> K;
  vvi cost(N, vi(M));
  for(auto &&e : cost)  for(auto &&ee : e)  cin >> ee;
  //solve();
  vvi dp(N+1, vi(K+1));
  //dp[i+1][j]:=i番目の国まで行ったとき、j円かかるか
  dp[0][0] = 1;
  bool ok;
  rep(i, N){
    ok = false;
    for(int j = 0; j <=K; ++j){
      rep(m, M){
        if(dp[i][j] && j+cost[i][m] <= K){
          dp[i+1][j+cost[i][m]] = 1;
          ok = true;
        }
      }
    }
    if(!ok)  break;
  }
  //output();
  if(!ok)  cout << -1 << endl;
  else{
    for(int j = K; 0 <= j; --j){
      if(dp[N][j]){
        cout << K-j << endl;
        break;
      }
    }
  }
}
0