#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++)
{
// cout << "a[i]=" << a[i] << ' ';
if (a[i]<k)
{
ok=0;
break;
}
for (int j=m-k+1; j<=m; j++)
{
b[j]--;
// cout << "subtract " << b[j] << ' ';
if (b[j]<0)
{
ok=0;
// cout << "out\n";
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=m; j>=1; j--)
{
if (b[j]<mid)
break;
sum+=b[j]-mid;
}
if (sum<=a[i]-k)
r=mid;
else
l=mid+1;
}
// cout << "l=" << l << ' ';
int sum=0;
for (int j=m; j>=1; j--)
{
if (b[j]<l)
break;
sum+=b[j]-l;
// cout << "subtract " << b[j]-l << ' ';
b[j]=l;
}
sort(b+1, b+m+1);
for (int j=m; j>=m-(a[i]-k-sum)+1; j--)
{
b[j]--;
// cout << "subtract " << b[j] << ' ';
}
sort(b+1, b+m+1);
// cout << "i=" << i << " over\n";
}
if (ok)
ans=min(ans, x2-x1);
}
if (ans==1e9)
cout << "Impossible";
else
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
11 ms |
212 KB |
Output is correct |
10 |
Correct |
19 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
320 KB |
Output is correct |
13 |
Correct |
32 ms |
212 KB |
Output is correct |
# |
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 |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
11 ms |
212 KB |
Output is correct |
10 |
Correct |
19 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
320 KB |
Output is correct |
13 |
Correct |
32 ms |
212 KB |
Output is correct |
14 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |