#include <bits/stdc++.h>
using namespace std;
int n, m, m1, m2, k;
int a[305], b[305];
int b1[305], b2[305];
int s;
bool pot1[305][90005];
int f2[305][90005];
void dps()
{
pot1[0][0] = true;
for (int i = 1; i <= m1; i++)
{
for (int j = 0; j <= 90000; j++)
{
pot1[i][j] = pot1[i - 1][j];
if (j - b1[i] >= 0)
pot1[i][j] |= pot1[i - 1][j - b1[i]];
}
}
for (int i = 0; i <= 300; i++)
for (int j = 0; j <= 90000; j++)
f2[i][j] = -1;
f2[0][0] = 0;
for (int i = 1; i <= m2; i++)
{
for (int j = 0; j <= 90000; j++)
{
f2[i][j] = f2[i - 1][j];
if (j - b2[i] >= 0)
if (f2[i - 1][j - b2[i]] != -1)
f2[i][j] = max(f2[i][j], 1 + f2[i - 1][j - b2[i]]);
}
}
}
int main()
{
cin >> n >> m >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], s += a[i];
for (int i = 1; i <= m; i++)
cin >> b[i];
for (int i = 1; i <= m; i++)
{
if (b[i] <= n)///!!!!
b1[++m1] = b[i];
else
b2[++m2] = b[i];
}
for (int i = 1; i <= n; i++)
{
if (a[i] < k)
{
cout << "Impossible";
return 0;
}
}
int sss1 = 0, sss2 = 0;
for (int i = 1; i <= m; i++)
sss1 += b[i], sss2 += min(b[i], n);
if (sss1 < s or sss2 < n * k)
{
cout << "Impossible";
return 0;
}
int ans = 1e9;
dps();
set<int> www;
int uu = 1e9;
for (int s1 = 0; s1 <= 90000; s1++)///!!!!
{
if (!pot1[m1][s1])
continue;
int new_uu;
if (s1 >= n * k)
new_uu = 0;
else
new_uu = (n * k - s1) / n + min(1, (n * k - s1) % n);
if (new_uu < uu)
{
for (int i = 0; i <= 90000; i++)
{
if (f2[m2][i] != -1 and f2[m2][i] >= new_uu and f2[m2][i] < uu)
www.insert(i);
}
}
uu = new_uu;
if (www.lower_bound(s - s1) == www.end())
continue;
int vll = *www.lower_bound(s - s1);
ans = min(ans, s1 + vll - s);
/*for (int i = max(0, s - s1); i <= 90000; i++)
if (i + s1 >= s and f2[m2][i] != -1 and s1 + n * f2[m2][i] >= n * k)
{
ans = min(ans, i + s1 - s);
break;
}*/
}
if (ans >= (int)1e8)
cout << "Impossible";
else
cout << ans;
return 0;
}
/**
imi fac pot1 si f2, apoi for prin i de la 0 la V^2 din pot1, imi mentin setul de i cu f2[i] >= cur si vad acolo un lower_bound
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
106240 KB |
Output is correct |
2 |
Correct |
41 ms |
106316 KB |
Output is correct |
3 |
Correct |
42 ms |
106308 KB |
Output is correct |
4 |
Correct |
44 ms |
106324 KB |
Output is correct |
5 |
Correct |
42 ms |
106612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
106240 KB |
Output is correct |
2 |
Correct |
41 ms |
106316 KB |
Output is correct |
3 |
Correct |
42 ms |
106308 KB |
Output is correct |
4 |
Correct |
44 ms |
106324 KB |
Output is correct |
5 |
Correct |
42 ms |
106612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
106368 KB |
Output is correct |
10 |
Correct |
45 ms |
106316 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
45 ms |
107632 KB |
Output is correct |
13 |
Correct |
46 ms |
107596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
129364 KB |
Output is correct |
2 |
Correct |
67 ms |
126292 KB |
Output is correct |
3 |
Correct |
73 ms |
132172 KB |
Output is correct |
4 |
Correct |
81 ms |
124756 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
64 ms |
124712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
106824 KB |
Output is correct |
2 |
Correct |
49 ms |
108116 KB |
Output is correct |
3 |
Correct |
48 ms |
109908 KB |
Output is correct |
4 |
Correct |
51 ms |
109908 KB |
Output is correct |
5 |
Correct |
0 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
106240 KB |
Output is correct |
2 |
Correct |
41 ms |
106316 KB |
Output is correct |
3 |
Correct |
42 ms |
106308 KB |
Output is correct |
4 |
Correct |
44 ms |
106324 KB |
Output is correct |
5 |
Correct |
42 ms |
106612 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
106368 KB |
Output is correct |
10 |
Correct |
45 ms |
106316 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
45 ms |
107632 KB |
Output is correct |
13 |
Correct |
46 ms |
107596 KB |
Output is correct |
14 |
Correct |
67 ms |
129364 KB |
Output is correct |
15 |
Correct |
67 ms |
126292 KB |
Output is correct |
16 |
Correct |
73 ms |
132172 KB |
Output is correct |
17 |
Correct |
81 ms |
124756 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
64 ms |
124712 KB |
Output is correct |
20 |
Correct |
45 ms |
106824 KB |
Output is correct |
21 |
Correct |
49 ms |
108116 KB |
Output is correct |
22 |
Correct |
48 ms |
109908 KB |
Output is correct |
23 |
Correct |
51 ms |
109908 KB |
Output is correct |
24 |
Correct |
0 ms |
444 KB |
Output is correct |
25 |
Correct |
59 ms |
107188 KB |
Output is correct |
26 |
Correct |
67 ms |
112464 KB |
Output is correct |
27 |
Correct |
63 ms |
112436 KB |
Output is correct |
28 |
Correct |
77 ms |
117332 KB |
Output is correct |
29 |
Correct |
66 ms |
107088 KB |
Output is correct |
30 |
Correct |
89 ms |
120820 KB |
Output is correct |