結果

問題 No.914 Omiyage
ユーザー rhincodon66rhincodon66
提出日時 2019-10-25 21:55:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,354 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 176,360 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 21:14:52
合計ジャッジ時間 2,479 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
4,348 KB
testcase_01 AC 14 ms
4,348 KB
testcase_02 AC 11 ms
4,348 KB
testcase_03 AC 14 ms
4,348 KB
testcase_04 AC 12 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 13 ms
4,348 KB
testcase_14 AC 14 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 5 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,1,0,-1},dy={-1,0,1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}


vector<vector<int>> a(10,vector<int>(10));
int n,m,k;


void dfs(vector<int>&b, int t, int sum, int o){
	if(t == o){
		b.pb(sum);
		return;
	}
	rep(i,m){
		dfs(b,t + 1, sum + a[t][i],o);
	}
}


int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	rep(i,n){
		rep(j,m){
			cin >> a[i][j];
		}
	}
	int o = min(n,5);
	vector<int> b;
	vector<int> c;
	dfs(b,0,0,o);
	int ans = 100000;
	if(n <= 5){
		rep(i,b.size()){
			if(b.at(i) <= k) ans = min(ans, k - b.at(i));
		}
		if(ans == 100000) cout << -1 << endl;
		else cout << ans << endl;
		return 0;
	}
	dfs(c,o,0,n);
	sort(b.begin(),b.end());
	sort(c.begin(),c.end());
	rep(i,c.size()){
		if(c.at(i) > k) break;
		int t = lower_bound(b.begin(),b.end(),k - c.at(i)) - b.begin();
		if(t == b.size()) t--;
		else if(b.at(t) != k - c.at(i)) t--;
		if(t >= 0) ans = min(ans, k - b.at(t) - c.at(i));
	}
	if(ans == 100000) cout << -1 << endl;
	else cout << ans << endl;
}
0