1 /**************************************************************
2 Problem: 1021
3 User: AsmDef
4 Language: C++
5 Result: Accepted
6 Time:488 ms
7 Memory:8648 kb
8 ****************************************************************/
9
10 /***********************************************************************/
11 /**********************By Asm.Def-Wu Jiaxin*****************************/
12 /***********************************************************************/
13 #include <cstdio>
14 #include <cstring>
15 #include <cstdlib>
16 #include <ctime>
17 #include <cctype>
18 #include <algorithm>
19 using namespace std;
20 template<class T>inline void getd(T &x){
21 char ch = getchar();bool neg = false;
22 while(!isdigit(ch) && ch != ‘-‘)ch = getchar();
23 if(ch == ‘-‘)ch = getchar(), neg = true;
24 x = ch - ‘0‘;
25 while(isdigit(ch = getchar()))x = x * 10 - ‘0‘ + ch;
26 if(neg)x = -x;
27 }
28 /***********************************************************************/
29 const int maxn = 1002, INF = 0x3f3f3f3f, val[6] = {1, 5, 10, 20, 50, 100};
30
31 int cnt[3][6], tot[6], Cur[3], Tar[2], Sum, f[2][maxn][maxn];//Tar[]: 目标状态
32
33 inline void init(){
34 int a, b, c;
35 getd(a), getd(b), getd(c);
36 for(int i = 0;i < 3;++i)for(int j = 5;j >= 0;--j){
37 getd(cnt[i][j]);
38 Cur[i] += cnt[i][j] * val[j];
39 tot[j] += cnt[i][j];
40 }
41 Sum = Cur[0] + Cur[1] + Cur[2];
42 Tar[0] = Cur[0] - a + c;
43 Tar[1] = Cur[1] - b + a;
44
45 if(Tar[0] < 0 || Tar[1] < 0 || Sum - Tar[0] - Tar[1] < 0){
46 printf("impossible\n");
47 exit(0);
48 }
49 }
50
51 #define UPD(a, b) (a = min(a, b) )
52 #include <cmath>
53
54 inline void work(){
55 const int rang = Sum + 1;
56 int i, j, k, a, b, t, tmp, da, db, cnta, cntb;
57 bool cur, las;
58 for(i = 0;i <= Sum;++i)memset(f[1][i], 0x3f, sizeof(int) * rang);
59 f[1][Cur[0]][Cur[1]] = 0;
60 for(i = 0;i < 6;++i){//枚举面值
61 cur = i & 1, las = cur ^ 1;
62 for(j = 0;j <= Sum;++j)memset(f[cur][j], 0x3f, sizeof(int) * rang);
63 for(j = 0;j <= Sum;++j){
64 t = Sum - j;
65 for(k = 0;k <= t;++k){//枚举A,B两人的当前资产
66 if(f[las][j][k] == INF)continue;
67 UPD(f[cur][j][k], f[las][j][k]);
68 for(a = 0;a <= tot[i];++a){
69 tmp = tot[i] - a;
70 for(b = 0;b <= tmp;++b){//枚举当前硬币的目标数量
71 da = a - cnt[0][i], db = b - cnt[1][i];
72 cnta = j + da * val[i], cntb = k + db * val[i];
73 if(cnta < 0 || cntb < 0 || cnta + cntb > Sum)continue;
74 UPD(f[cur][cnta][cntb], f[las][j][k] + (abs(da) + abs(db) + abs(da + db)) / 2);
75 }
76 }
77 }
78 }
79 }
80 if(f[cur][Tar[0]][Tar[1]] == INF)printf("impossible\n");
81 else printf("%d\n", f[cur][Tar[0]][Tar[1]]);
82 }
83
84 int main(){
85 #ifdef DEBUG
86 freopen("test.txt", "r", stdin);
87 #elif not defined ONLINE_JUDGE
88 freopen(".in", "r", stdin);
89 freopen(".out", "w", stdout);
90 #endif
91 init();
92 work();
93
94 #ifdef DEBUG
95 printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
96 #endif
97 return 0;
98 }