Submission #1320768

#TimeUsernameProblemLanguageResultExecution timeMemory
1320768mirasmKitchen (BOI19_kitchen)C++20
51 / 100
12 ms1080 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long

const int N = 300 + 4;


void fun () {  
	int n, k, m;
	cin >> n >> m >> k;
	vector<int> a(n + 1), b(m + 1);
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= m; i++) cin >> b[i];
	
	if (k == 1) {
		int s1 = 0;
		for (int i = 1; i <= m; i++) s1 += b[i];

		vector<int> dp(s1 + 1);
		dp[0] = 1;
		for (int i = 1; i <= m; i++) {
			for (int j = s1; j >= b[i]; j--) {
				dp[j] |= dp[j - b[i]];
			}
		}

		int s = 0;
		for (int i = 1; i <= n; i++) {
			s += a[i];
		}
		if (s1 < s) {
			cout << "Impossible";
			return;
		}

		for (int i = s; i <= s1; i++) {
			if (dp[i]) {
				cout << i - s;
				return;
			}
		}
	}
	int ans = 1e18, s = 0;
	for (int i = 1; i <= n; i++) s += a[i];
	
	for (int i = 1; i <= n; i++) {
		if (a[i] < k) {
			cout << "Impossible";
			return;
		}
	}

	for (int mask = 0; mask < (1 << m); mask++) {
		if (__builtin_popcount(mask) < k) continue;
		int s1 = 0;
		int res = k;
		vector<int> vt;
		for (int i = 0; i < m; i++) {
			if (mask >> i & 1) {
				s1 += b[i + 1];
				if (b[i + 1] >= n) {
					res--;		
				}
				else {
					vt.push_back(b[i + 1]);
				}
			}
		}

		if (s1 < s) continue;
		sort(vt.rbegin(), vt.rend());
		int now = 0;
		for (auto e : vt) now += e;
		res -= now / n;	
		if (res <= 0) {
			ans = min(ans, s1 - s);

		}
			
	}
	if (ans == 1e18) cout << "Impossible";
	else cout << ans << endl;

}

signed main () {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int tt = 1;
        //cin >> tt;
        while (tt--) fun(); 
        return 0;

}



#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...