#include <bits/stdc++.h>
using namespace std;
int a[305], B[305], b[305];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, M, k, x1=0, ans=1e9;
cin >> n >> M >> k;
for (int i=1; i<=n; i++)
{
cin >> a[i];
x1+=a[i];
}
for (int i=1; i<=M; i++)
cin >> B[i];
for (int z=1; z<(1<<M); z++)
{
int m=0, x2=0;
for (int zz=0; zz<M; zz++)
{
if (z&(1<<zz))
{
m++;
b[m]=B[zz+1];
x2+=b[m];
}
}
bool ok=1;
if (m<k || x1>x2)
{
ok=0;
continue;
}
sort(b+1, b+m+1);
for (int i=1; i<=n; i++)
{
if (a[i]<k)
{
ok=0;
break;
}
for (int j=m-k+1; j<=m; j++)
{
b[j]--;
if (b[j]<0)
{
ok=0;
break;
}
}
if (!ok)
break;
sort(b+1, b+m+1);
int l=0, r=300;
while (l<r)
{
int mid=(l+r)/2, sum=0;
for (int j=1; j<=m; j++)
{
if (b[j]<mid)
break;
sum+=b[j]-mid;
}
if (sum<=a[i]-k)
r=mid;
else
l=mid+1;
}
int sum=0;
for (int j=m; j>=1; j--)
{
if (b[j]<l)
break;
sum+=b[j]-l;
}
sort(b+1, b+m+1);
for (int j=m; j>=m-(a[j]-k-sum)+1; j--)
b[j]--;
sort(b+1, b+m+1);
}
if (ok)
ans=min(ans, x2-x1);
}
if (ans==1e9)
cout << "Impossible";
else
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |