#P4026. [SHOI2008] 循环的债务
[SHOI2008] 循环的债务
Description
Alice, Bob, and Cynthia are always troubled by the messy debts among them. One day, they decide to sit together and settle everything. However, checking the authenticity of banknotes is troublesome, so they decide to exchange as little cash as possible when paying off their debts.
For example, Alice owes Bob yuan, while Cynthia owes neither of them anything. Suppose Alice has one yuan note, Bob has three yuan notes and ten yuan notes, and Cynthia has three yuan notes. A straightforward way is: Alice gives the yuan to Bob, and Bob gives change back to Alice. In total, banknotes are exchanged. But this is not optimal. The optimal way is: Alice gives the to Cynthia, Cynthia gives two notes to Alice and one note to Bob, and Bob gives one note to Cynthia. In this case, only banknotes are exchanged.
Before long, they find this is a hard problem, so they ask you, who are good at math, to solve it for them.
Input Format
The first line contains three integers: , , (), where
is the amount Alice owes Bob (if is negative, it means Bob owes Alice),
is the amount Bob owes Cynthia (if is negative, it means Cynthia owes Bob),
is the amount Cynthia owes Alice (if is negative, it means Alice owes Cynthia).
Then there are three lines, each containing 6 natural numbers:
is the number of yuan banknotes Alice has, is the number of yuan banknotes Bob has, and so on. We also guarantee , , , and the total face value of all banknotes owned by the three people does not exceed .
Output Format
If the debts can be fully settled, output the minimum number of banknotes that need to be exchanged. If they cannot be settled, output impossible.
10 0 0
0 1 0 0 0 0
0 0 0 3 0 10
0 0 3 0 0 0
5
-10 -10 -10
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0
Hint
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号