This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e4;
const ll inf = 1e17;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 500;
int n, m, k;
int a[mxn];
int b[mxn];
bool done[mxn*mxn];
map<pair<pii, pii>, bool> mapa;
void solve(int h, int pos, int sum, int p){
if(mapa[{{h, pos}, {sum,p}}]) return;
mapa[{{h, pos}, {sum,p}}] = true;
if(h == k-1 && pos == n){
done[sum] = true;
}
if(p == m) return;
int bound = n;
if(h == k-1) bound = n-pos;
int extr = 0;
int add = b[p];
if(b[p] > bound){
extr = b[p]-bound;
add = bound;
}
if(pos + add > n){
solve(h+1, pos+add-n, sum+extr, p+1);
}
else{
solve(h, pos+add, sum + extr, p+1);
}
solve(h, pos, sum, p+1);
}
void solve(){
cin >> n >> m >> k;
int rem = 0;
fr(i, 0, n){
cin >> a[i];
rem += a[i]-k;
}
fr(i, 0, m){
cin >> b[i];
}
fr(i, 0, n){
if(a[i] < k){
cout<<"Impossible"<<endl;
return;
}
}
solve(0, 0, 0, 0);
int ans = -1;
fr(i, rem, mxn*mxn) if(done[i]){ ans = i; break;}
if(ans == -1) cout<<"Impossible"<<endl;
else cout<<ans-rem<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |